package com.ruoyi.factory.buyprice.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.buyprice.domain.*;
import com.ruoyi.factory.buyprice.mapper.*;
import com.ruoyi.factory.buyprice.service.BuMaHandleService;
import com.ruoyi.factory.common.utils.IdGen;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName BurateHandleServiceImpl
 * @Description: TODO
 * @Author LiuJiaji
 * @Date 2024/5/29 09:48
 **/
@Service
public class BuMaHandleServiceImpl  implements BuMaHandleService {
    protected final Logger logger = LoggerFactory.getLogger(BuMaHandleServiceImpl.class);
    @Autowired
    private BurateItemMapper burateItemMapper;
    @Autowired
    private BurateMonthMapper burateMonthMapper;
    @Autowired
    private BumawhHeadMapper bumawhHeadMapper;
    @Autowired
    private BumawhChaBodyMapper bumawhChaBodyMapper;
    @Autowired
    private BumawhRawBodyMapper bumawhRawBodyMapper;
    @Autowired
    private BumaAvepriMapper bumaAvepriMapper;
    @Autowired
    private BumapriMapper bumapriMapper;
    @Autowired
    private BumaStatisticsDayMapper bumaStatisticsDayMapper;
    @Autowired
    private BumaStatisticsMonthMapper bumaStatisticsMonthMapper;
    @Autowired
    private BumaStatisticsQuarterMapper bumaStatisticsQuarterMapper;
    @Autowired
    private BumaStatisticsHalfyearMapper bumaStatisticsHalfyearMapper;
    @Autowired
    private BumaStatisticsYearMapper bumaStatisticsYearMapper;

    /**
     * @author: Liujiaji
     * @date: 2024/5/29 10:45
     * @description: TODO 根据保存日汇率信息处理月平均汇率
     * @param: [request]
     * @return: com.ruoyi.common.core.domain.AjaxResult
     **/
    public AjaxResult iteamTomonth(String request){
        JSONObject jsonObject = JSONObject.fromObject(request);
        String id = jsonObject.optString("id");
        //通过id查询数据库获取当前月时间, 根据时间求平均值
        BurateItem burateItem = burateItemMapper.selectBurateItemById(Long.valueOf(id));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        if(null != burateItem){
            Date ratedate = burateItem.getRatedate();
            BurateItem burateItem1 = new BurateItem();
            burateItem1.setRatedate(ratedate);
            List<BurateItem> burateItems = burateItemMapper.selectBurateItemList2(burateItem1);
            if(!burateItems.isEmpty()){
                BigDecimal bigDecimal1 = new BigDecimal(0);
                BigDecimal bigDecimal2 = new BigDecimal(0);
                for (BurateItem item : burateItems) {
                    BigDecimal rate = item.getRate();
                    bigDecimal1 = bigDecimal1.add(rate);
                    bigDecimal2 = bigDecimal2.add(new BigDecimal(1));
                }
                BigDecimal divide = bigDecimal1.divide(bigDecimal2,6,BigDecimal.ROUND_HALF_UP);
                BurateMonth burateMonth = new BurateMonth();
                String format = sdf.format(ratedate);
                try {
                    Date parse = sdf.parse(format);
                    burateMonth.setRatedate(parse);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                List<BurateMonth> burateMonths = burateMonthMapper.selectBurateMonthList(burateMonth);
                burateMonth.setRate(divide);
                if(burateMonths.isEmpty()){
                    burateMonthMapper.insertBurateMonth(burateMonth);
                }else{
                    burateMonthMapper.updateBurateMonth2(burateMonth);
                }

            }
        }
        return null;
    }

    public void bumawhadd(String date){
        logger.info("采购行情当天价格维护更新执行开始");
        BumawhHead paramb = new BumawhHead();
        if(StringUtils.isNotBlank(date)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = null;
            try {
                parse = sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            paramb.setSedate(parse);
        }else{
            paramb.setSedate(new Date());
        }
        List<BumawhHead> bumawhHeads = bumawhHeadMapper.selectBumawhHeadList(paramb);
        for (BumawhHead bumawhHead : bumawhHeads) {
            Long idl = bumawhHead.getId();
//            JSONObject jsonObject = JSONObject.fromObject(request);
            //查询信息
            String id = String.valueOf(idl);
//            String type = jsonObject.optString("type");
            //更新价格
            //1.换算渠道价格
//            bumawhaup(id);
            // 插入日折线图数据信息
            updateBumaStatisticsDay(id);
            //插入月折线图数据信息
            updateBumaStatisticsMonth(id);
            //插入季度折线图数据信息
            updateBumaStatisticsQuarter(id);
            //插入半年折线图数据信息
            updateBumaStatisticsHalfYear(id);
            //插入年折线图数据信息
            updateBumaStatisticsYear(id);
        }
    }
    //更新维护渠道上游物料日期
    public void bumawhaddupdate(String request){
        JSONObject jsonObject = JSONObject.fromObject(request);
        //查询信息
        String id = jsonObject.optString("id");
        String code = jsonObject.optString("code");
        Long sedate = Long.valueOf(jsonObject.optString("sedate"));

        long javaTimeInMillis = (long) ((sedate-25569) * 24 * 60 * 60 * 1000);
        Date date = new Date(javaTimeInMillis);
        // 定义日期格式化模板，例如 "yyyy-MM-dd" 格式表示年-月-日
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(date);
        BumawhChaBody paracha = new BumawhChaBody();
        Long aLong = Long.valueOf(id);
        paracha.setBumaHeadId(aLong);
        paracha.setSedate(date);
        bumawhChaBodyMapper.updateBumawhChaBodybyheadid(paracha);
        BumawhRawBody pararaw = new BumawhRawBody();
        pararaw.setBumaHeadId(aLong);
        pararaw.setSedate(date);
        bumawhRawBodyMapper.updateBumawhRawBodybyheadid(pararaw);
        //价格换算,单价*数量=总价 总价相加/总数量=单价
        List<BumawhChaBody> bumawhChaBodies = bumawhChaBodyMapper.selectBumawhChaBodyList(paracha);
        BigDecimal totalprice = new BigDecimal(0);
        BigDecimal totalbuyAmount = new BigDecimal(0);
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies) {
            BigDecimal buyPriceYb = bumawhChaBody.getBuyPriceYb();
            BigDecimal buyAmount = bumawhChaBody.getBuyAmount();
            if(null == buyPriceYb && null == buyAmount)continue;
            totalprice = totalprice.add(buyPriceYb.multiply(buyAmount));
            totalbuyAmount = totalbuyAmount.add(buyAmount);
        }
        BumawhHead parambum = new BumawhHead();
        parambum.setBuyamounth(totalbuyAmount);
        if(null != totalbuyAmount && totalbuyAmount.compareTo(BigDecimal.ZERO)>0){
            BigDecimal divide = totalprice.divide(totalbuyAmount, 3, BigDecimal.ROUND_HALF_UP);
            parambum.setBuyPriceh(divide);
        }
        parambum.setId(aLong);
        bumawhHeadMapper.updateBumawhHead(parambum);
        //1.换算渠道价格
        bumawhaup(id);
        logger.info("采购行情当天价格维护更新执行结束---");
    }

    public void updateBumaStatisticsYear(String id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        BumawhHead bumawhHead = bumawhHeadMapper.selectBumawhHeadById2(id);
        if(null != bumawhHead){
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDay.setDate2(bumawhHead.getSedate());
//            bumaStatisticsDay.setManame(bumawhHead.getBomname());
//            List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(!bumaStatisticsDays.isEmpty() && bumaStatisticsDays.size()>0){
//                BumaStatisticsDay bumaStatisticsDay1 = bumaStatisticsDays.get(0);
//                bumaStatisticsDay1.setCode(bumawhHead.getCode());
//                bumaStatisticsDay1.setPrice(bumawhHead.getContractChaPrice());
//                bumaStatisticsDayMapper.updateBumaStatisticsDay(bumaStatisticsDay1);
//            }
            String format = sdf.format(bumawhHead.getSedate());
            //格式时间
            String[] split = format.split("-");
            format=split[0]+"年";
//            Date parse = null;
//            try {
//                parse = sdf.parse(format);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }

            //上游原料信息插入
            List<BumawhRawBody> bumawhRawBodyList = bumawhHead.getBumawhRawBodyList();
            for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
                BumaStatisticsQuarter BumaStatisticsQuarter2 = new BumaStatisticsQuarter();
                BumaStatisticsQuarter2.setType("2");

                BumaStatisticsQuarter2.setDate(format);
                BumaStatisticsQuarter2.setManame(bumawhRawBody.getRawname());
                List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(BumaStatisticsQuarter2);
                if(!bumaStatisticsQuarters.isEmpty() && bumaStatisticsQuarters.size()>0){
                    BumaStatisticsQuarter statisticsQuarter = bumaStatisticsQuarters.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    BigDecimal bigDecimal = new BigDecimal(0);
                    BigDecimal num = new BigDecimal(0);
                    for (int i = 1; i < 13; i++) {
                        String j = String.format("%02d", i);
                        format1 = split1[0]+"-"+j;
                        Map<String, String> pmap = new HashMap<>();
                        pmap.put("rawname",bumawhRawBody.getRawname());
                        pmap.put("sedate",format1);
                        BumawhRawBody bumawhRawBody1 = bumawhHeadMapper.selectAveRawPrice(pmap);
                        if(null != bumawhRawBody1 &&
                                null != bumawhRawBody1.getMarketPrice()&&
                                        null != bumawhRawBody1.getRatio()
                        ){
                            bigDecimal = bigDecimal.add(bumawhRawBody1.getMarketPrice());
                            num = num.add(new BigDecimal(bumawhRawBody1.getRatio()));
                        }
                    }
                    if(bigDecimal.compareTo(new BigDecimal(0))>0 &&
                            num.compareTo(new BigDecimal(0))>0
                    ){
                        BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                        statisticsQuarter.setPrice(divide);
                    }
                    bumaStatisticsQuarterMapper.updateBumaStatisticsQuarter(statisticsQuarter);

                }
            }

            List<BumawhChaBody> bumawhChaBodyList = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
                BumaStatisticsYear BumaStatisticsYear2 = new BumaStatisticsYear();
                BumaStatisticsYear2.setType("3");
                BumaStatisticsYear2.setDate(format);
                BumaStatisticsYear2.setManame(bumawhChaBody.getChaname());
                List<BumaStatisticsYear> bumaStatisticsYears = bumaStatisticsYearMapper.selectBumaStatisticsYearList(BumaStatisticsYear2);
                if (!bumaStatisticsYears.isEmpty() && bumaStatisticsYears.size() > 0) {
                    BumaStatisticsYear statisticsYear = bumaStatisticsYears.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    BigDecimal bigDecimal = new BigDecimal(0);
                    BigDecimal num = new BigDecimal(0);
                    for (int i = 1; i < 13; i++) {
                        String j = String.format("%02d", i);
                        format1 = split1[0] + "-" + j;
                        Map<String, String> pmap = new HashMap<>();
                        pmap.put("chaname", bumawhChaBody.getChaname());
                        pmap.put("sedate", format1);
                        BumawhChaBody bumawhChaBody1 = bumawhHeadMapper.selectAveChaPrice2(pmap);
                        if(null != bumawhChaBody1 &&
                                null != bumawhChaBody1.getMarketPriceBb() &&
                                        null != bumawhChaBody1.getBuyAmount()
                        ){
                            bigDecimal = bigDecimal.add(bumawhChaBody1.getMarketPriceBb());
                            num = num.add(bumawhChaBody1.getBuyAmount());
                        }
                    }
                    if(bigDecimal.compareTo(new BigDecimal(0))>0 &&
                            num.compareTo(new BigDecimal(0))>0
                    ){
                        BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                        statisticsYear.setPrice(divide);
                    }
                    bumaStatisticsYearMapper.updateBumaStatisticsYear(statisticsYear);
                }
            }

            List<BumawhChaBody> bumawhChaBodyList2 = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList2) {
                BumaStatisticsYear BumaStatisticsYear2 = new BumaStatisticsYear();
                BumaStatisticsYear2.setType("1");
                BumaStatisticsYear2.setDate(format);
                BumaStatisticsYear2.setManame(bumawhChaBody.getChaname()+"采购价");
                BumaStatisticsYear2.setCode(bumawhChaBody.getChaname());
                List<BumaStatisticsYear> bumaStatisticsYears = bumaStatisticsYearMapper.selectBumaStatisticsYearList(BumaStatisticsYear2);
                if(!bumaStatisticsYears.isEmpty() && bumaStatisticsYears.size()>0){
                    BumaStatisticsYear statisticsYear = bumaStatisticsYears.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    BigDecimal bigDecimal = new BigDecimal(0);
                    BigDecimal num = new BigDecimal(0);
                    for (int i = 1; i < 13; i++) {
                        String j = String.format("%02d", i);
                        format1 = split1[0] + "-" + j;
                        Map<String, String> pmap = new HashMap<>();
                        pmap.put("chaname", bumawhChaBody.getChaname());
                        pmap.put("sedate", format1);
                        BumawhChaBody bumawhChaBody1 = bumawhHeadMapper.selectAveChaPrice(pmap);
                        if(null != bumawhChaBody1 &&
                                null != bumawhChaBody1.getBuyPriceBb() &&
                                        null != bumawhChaBody1.getBuyAmount()
                        ){
                            bigDecimal = bigDecimal.add(bumawhChaBody1.getBuyPriceBb());
                            num = num.add(bumawhChaBody1.getBuyAmount());
                        }
                    }
                    if(bigDecimal.compareTo(new BigDecimal(0))>0 &&
                            num.compareTo(new BigDecimal(0))>0
                    ){
                        BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                        statisticsYear.setPrice(divide);
                    }
                    bumaStatisticsYearMapper.updateBumaStatisticsYear(statisticsYear);
                }
            }

//            List<BumawhErpmaBody> bumawhErpmaBodyList = bumawhHead.getBumawhErpmaBodyList();
            String code = bumawhHead.getCode();
            BumawhHead bumawhHead1 = bumawhHeadMapper.selectBumaHeadByCode(code);
            BumawhHead bumawhHead2 = bumawhHeadMapper.selectBumaHeadById2(String.valueOf(bumawhHead1.getId()));
            List<BumawhErpmaBody> bumawhErpmaBodyList1 = bumawhHead2.getBumawhErpmaBodyList();
            for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList1) {
                BumaStatisticsYear statisticsYear = new BumaStatisticsYear();
                statisticsYear.setType("4");
                statisticsYear.setDate(format);
                statisticsYear.setManame(bumawhErpmaBody.getErpname());
                statisticsYear.setCode(bumawhErpmaBody.getErpcode());
                List<BumaStatisticsYear> bumaStatisticsYears = bumaStatisticsYearMapper.selectBumaStatisticsYearList(statisticsYear);
                if(!bumaStatisticsYears.isEmpty() && bumaStatisticsYears.size()>0){
                    BumaStatisticsYear statisticsYear1 = bumaStatisticsYears.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    BigDecimal bigDecimal = new BigDecimal(0);
                    BigDecimal num = new BigDecimal(0);
                    for (int i = 1; i < 13; i++) {
                        String j = String.format("%02d", i);
                        format1 = split1[0] + "-" + j;
                        Bumapri bumapri1 = new Bumapri();
                        bumapri1.setManame(bumawhErpmaBody.getErpname());
                        bumapri1.setMacode(bumawhErpmaBody.getErpcode());
                        bumapri1.setPbilldate(format1);
                        Bumapri bumapri = bumapriMapper.selectBumaByMon(bumapri1);
                        if(null != bumapri &&
                                null != bumapri.getNassistnum() &&
                                null != bumapri.getNtaxmny()
                        ){
                            num = num.add(bumapri.getNassistnum());
                            bigDecimal = bigDecimal.add(bumapri.getNtaxmny());
                        }
                        if(bigDecimal.compareTo(new BigDecimal(0))>0 &&
                                num.compareTo(new BigDecimal(0))>0
                        ){
                            //金额/数量
                            BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                            statisticsYear1.setPrice(divide);
                        }
                    }
                    bumaStatisticsYearMapper.updateBumaStatisticsYear(statisticsYear1);
                }
            }
        }
    }

    public void updateBumaStatisticsHalfYear(String id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        BumawhHead bumawhHead = bumawhHeadMapper.selectBumawhHeadById2(id);
        if(null != bumawhHead){
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDay.setDate2(bumawhHead.getSedate());
//            bumaStatisticsDay.setManame(bumawhHead.getBomname());
//            List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(!bumaStatisticsDays.isEmpty() && bumaStatisticsDays.size()>0){
//                BumaStatisticsDay bumaStatisticsDay1 = bumaStatisticsDays.get(0);
//                bumaStatisticsDay1.setCode(bumawhHead.getCode());
//                bumaStatisticsDay1.setPrice(bumawhHead.getContractChaPrice());
//                bumaStatisticsDayMapper.updateBumaStatisticsDay(bumaStatisticsDay1);
//            }
            String format = sdf.format(bumawhHead.getSedate());
            //格式时间
            String[] split = format.split("-");
            if("01".equals(split[1]) || "02".equals(split[1]) || "03".equals(split[1]) ||
                    "04".equals(split[1]) || "05".equals(split[1]) || "06".equals(split[1])
            ){
                format=split[0]+"年上半年";
            }else if("07".equals(split[1]) || "08".equals(split[1]) || "09".equals(split[1]) ||
                    "10".equals(split[1]) || "11".equals(split[1]) || "12".equals(split[1])
            ){
                format=split[0]+"年下半年";
            }
//            Date parse = null;
//            try {
//                parse = sdf.parse(format);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }

            //上游原料信息插入
            List<BumawhRawBody> bumawhRawBodyList = bumawhHead.getBumawhRawBodyList();
            for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
                BumaStatisticsHalfyear BumaStatisticsHalfyear2 = new BumaStatisticsHalfyear();
                BumaStatisticsHalfyear2.setType("2");

                BumaStatisticsHalfyear2.setDate(format);
                BumaStatisticsHalfyear2.setManame(bumawhRawBody.getRawname());
                List<BumaStatisticsHalfyear> bumaStatisticsHalfyears = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(BumaStatisticsHalfyear2);
                if(!bumaStatisticsHalfyears.isEmpty() && bumaStatisticsHalfyears.size()>0){
                    BumaStatisticsHalfyear statisticsHalfyear = bumaStatisticsHalfyears.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    int c = 0;
                    if("01".equals(split1[1]) || "02".equals(split1[1]) || "03".equals(split1[1]) ||
                            "04".equals(split1[1]) || "05".equals(split1[1]) || "06".equals(split1[1])) c=1;
                    if("07".equals(split1[1]) || "08".equals(split1[1]) || "09".equals(split1[1]) ||
                            "10".equals(split1[1]) || "11".equals(split1[1]) || "12".equals(split1[1])) c=7;
                    if(c!=0){
                        BigDecimal bigDecimal = new BigDecimal(0);
                        BigDecimal num = new BigDecimal(0);
                        for (int i = 1; i < 7; i++) {
                            String j = String.format("%02d", c);
                            c++;
                            format1 = split1[0]+"-"+j;
                            Map<String, String> pmap = new HashMap<>();
                            pmap.put("rawname",bumawhRawBody.getRawname());
                            pmap.put("sedate",format1);
                            BumawhRawBody bumawhRawBody1 = bumawhHeadMapper.selectAveRawPrice(pmap);
                            if(null != bumawhRawBody1 &&
                                    null != bumawhRawBody1.getMarketPrice() &&
                                    null != bumawhRawBody1.getRatio()
                            ){
                                bigDecimal = bigDecimal.add(bumawhRawBody1.getMarketPrice());
                                num = num.add(new BigDecimal(bumawhRawBody1.getRatio()));
                            }
                        }
                        if(bigDecimal.compareTo(new BigDecimal(0))>0 &&
                                num.compareTo(new BigDecimal(0))>0
                        ){
                            BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                            statisticsHalfyear.setPrice(divide);
                        }
                        bumaStatisticsHalfyearMapper.updateBumaStatisticsHalfyear(statisticsHalfyear);
                    }
                }
            }

            List<BumawhChaBody> bumawhChaBodyList = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
                BumaStatisticsHalfyear BumaStatisticsHalfyear2 = new BumaStatisticsHalfyear();
                BumaStatisticsHalfyear2.setType("3");
                BumaStatisticsHalfyear2.setDate(format);
                BumaStatisticsHalfyear2.setManame(bumawhChaBody.getChaname());
                List<BumaStatisticsHalfyear> bumaStatisticsHalfyears = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(BumaStatisticsHalfyear2);
                if (!bumaStatisticsHalfyears.isEmpty() && bumaStatisticsHalfyears.size() > 0) {
                    BumaStatisticsHalfyear statisticsHalfyear = bumaStatisticsHalfyears.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    int c = 0;
                    if ("01".equals(split1[1]) || "02".equals(split1[1]) || "03".equals(split1[1]) ||
                            "04".equals(split1[1]) || "05".equals(split1[1]) || "06".equals(split1[1])
                    ) c = 1;
                    if ("07".equals(split1[1]) || "08".equals(split1[1]) || "09".equals(split1[1]) ||
                            "10".equals(split1[1]) || "11".equals(split1[1]) || "12".equals(split1[1])
                    ) c = 7;
                    if (c != 0) {
                        BigDecimal bigDecimal = new BigDecimal(0);
                        BigDecimal num = new BigDecimal(0);
                        for (int i = 1; i < 7; i++) {
                            String j = String.format("%02d", c);
                            c++;
                            format1 = split1[0] + "-" + j;
                            Map<String, String> pmap = new HashMap<>();
                            pmap.put("chaname", bumawhChaBody.getChaname());
                            pmap.put("sedate", format1);
                            BumawhChaBody bumawhChaBody1 = bumawhHeadMapper.selectAveChaPrice2(pmap);
                            if(null != bumawhChaBody1 &&
                                    null != bumawhChaBody1.getMarketPriceBb() &&
                                    null != bumawhChaBody1.getBuyAmount()
                            ){
                                bigDecimal = bigDecimal.add(bumawhChaBody1.getMarketPriceBb());
                                num = num.add(bumawhChaBody1.getBuyAmount());
                            }
                        }
                        if(bigDecimal.compareTo(new BigDecimal(0))>0 &&
                                num.compareTo(new BigDecimal(0))>0
                        ){
                            BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                            statisticsHalfyear.setPrice(divide);
                        }
                        bumaStatisticsHalfyearMapper.updateBumaStatisticsHalfyear(statisticsHalfyear);
                    }
                }
            }

            List<BumawhChaBody> bumawhChaBodyList2 = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList2) {
                BumaStatisticsHalfyear BumaStatisticsHalfyear2 = new BumaStatisticsHalfyear();
                BumaStatisticsHalfyear2.setType("1");
                BumaStatisticsHalfyear2.setDate(format);
                BumaStatisticsHalfyear2.setManame(bumawhChaBody.getChaname()+"采购价");
                BumaStatisticsHalfyear2.setCode(bumawhChaBody.getChaname());
                List<BumaStatisticsHalfyear> bumaStatisticsHalfyears = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(BumaStatisticsHalfyear2);
                if(!bumaStatisticsHalfyears.isEmpty() && bumaStatisticsHalfyears.size()>0){
                    BumaStatisticsHalfyear statisticsHalfyear = bumaStatisticsHalfyears.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    int c = 0;
                    if ("01".equals(split1[1]) || "02".equals(split1[1]) || "03".equals(split1[1]) ||
                            "04".equals(split1[1]) || "05".equals(split1[1]) || "06".equals(split1[1])
                    ) c = 1;
                    if ("07".equals(split1[1]) || "08".equals(split1[1]) || "09".equals(split1[1]) ||
                            "10".equals(split1[1]) || "11".equals(split1[1]) || "12".equals(split1[1])
                    ) c = 7;
                    if (c != 0) {
                        BigDecimal bigDecimal = new BigDecimal(0);
                        BigDecimal num = new BigDecimal(0);
                        for (int i = 1; i < 7; i++) {
                            String j = String.format("%02d", c);
                            c++;
                            format1 = split1[0] + "-" + j;
                            Map<String, String> pmap = new HashMap<>();
                            pmap.put("chaname", bumawhChaBody.getChaname());
                            pmap.put("sedate", format1);
                            BumawhChaBody bumawhChaBody1 = bumawhHeadMapper.selectAveChaPrice(pmap);
                            if(null != bumawhChaBody1 &&
                                    null != bumawhChaBody1.getBuyPriceBb() &&
                                    null != bumawhChaBody1.getBuyAmount()
                            ){
                                bigDecimal = bigDecimal.add(bumawhChaBody1.getBuyPriceBb());
                                num = num.add(bumawhChaBody1.getBuyAmount());
                            }
                        }
                        if(bigDecimal.compareTo(new BigDecimal(0))>0 &&
                                num.compareTo(new BigDecimal(0))>0
                        ){
                            BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                            statisticsHalfyear.setPrice(divide);
                        }
                        bumaStatisticsHalfyearMapper.updateBumaStatisticsHalfyear(statisticsHalfyear);
                    }
                }
            }

//            List<BumawhErpmaBody> bumawhErpmaBodyList = bumawhHead.getBumawhErpmaBodyList();
            String code = bumawhHead.getCode();
            BumawhHead bumawhHead1 = bumawhHeadMapper.selectBumaHeadByCode(code);
            BumawhHead bumawhHead2 = bumawhHeadMapper.selectBumaHeadById2(String.valueOf(bumawhHead1.getId()));
            List<BumawhErpmaBody> bumawhErpmaBodyList1 = bumawhHead2.getBumawhErpmaBodyList();
            for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList1) {
                BumaStatisticsHalfyear statisticsHalfyear = new BumaStatisticsHalfyear();
                statisticsHalfyear.setType("4");
                statisticsHalfyear.setDate(format);
                statisticsHalfyear.setManame(bumawhErpmaBody.getErpname());
                statisticsHalfyear.setCode(bumawhErpmaBody.getErpcode());
                List<BumaStatisticsHalfyear> bumaStatisticsHalfyears = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(statisticsHalfyear);
                if(!bumaStatisticsHalfyears.isEmpty() && bumaStatisticsHalfyears.size()>0){
                    BumaStatisticsHalfyear statisticsHalfyear1 = bumaStatisticsHalfyears.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    int c = 0;
                    if ("01".equals(split1[1]) || "02".equals(split1[1]) || "03".equals(split1[1]) ||
                            "04".equals(split1[1]) || "05".equals(split1[1]) || "06".equals(split1[1])
                    ) c = 1;
                    if ("07".equals(split1[1]) || "08".equals(split1[1]) || "09".equals(split1[1]) ||
                            "10".equals(split1[1]) || "11".equals(split1[1]) || "12".equals(split1[1])
                    ) c = 7;
                    if (c != 0) {
                        BigDecimal bigDecimal = new BigDecimal(0);
                        BigDecimal num = new BigDecimal(0);
                        for (int i = 1; i < 7; i++) {
                            String j = String.format("%02d", c);
                            c++;
                            format1 = split1[0] + "-" + j;
                            Bumapri bumapri1 = new Bumapri();
                            bumapri1.setManame(bumawhErpmaBody.getErpname());
                            bumapri1.setMacode(bumawhErpmaBody.getErpcode());
                            bumapri1.setPbilldate(format1);
                            Bumapri bumapri = bumapriMapper.selectBumaByMon(bumapri1);
                            if(null != bumapri &&
                                    null != bumapri.getNassistnum() &&
                                    null != bumapri.getNtaxmny()
                            ){
                                num = num.add(bumapri.getNassistnum());
                                bigDecimal = bigDecimal.add(bumapri.getNtaxmny());
                            }
                            if(bigDecimal.compareTo(new BigDecimal(0))>0 &&
                                    num.compareTo(new BigDecimal(0))>0
                            ){
                                //金额/数量
                                BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                                statisticsHalfyear1.setPrice(divide);
                            }
                        }
                        bumaStatisticsHalfyearMapper.updateBumaStatisticsHalfyear(statisticsHalfyear1);
                    }
                }
            }
        }
    }

    public void updateBumaStatisticsQuarter(String id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        BumawhHead bumawhHead = bumawhHeadMapper.selectBumawhHeadById2(id);
        if(null != bumawhHead){
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDay.setDate2(bumawhHead.getSedate());
//            bumaStatisticsDay.setManame(bumawhHead.getBomname());
//            List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(!bumaStatisticsDays.isEmpty() && bumaStatisticsDays.size()>0){
//                BumaStatisticsDay bumaStatisticsDay1 = bumaStatisticsDays.get(0);
//                bumaStatisticsDay1.setCode(bumawhHead.getCode());
//                bumaStatisticsDay1.setPrice(bumawhHead.getContractChaPrice());
//                bumaStatisticsDayMapper.updateBumaStatisticsDay(bumaStatisticsDay1);
//            }
            String format = sdf.format(bumawhHead.getSedate());
            //格式时间
            String[] split = format.split("-");
            if("01".equals(split[1]) || "02".equals(split[1]) || "03".equals(split[1])){
                format=split[0]+"年一季度";
            }else if("04".equals(split[1]) || "05".equals(split[1]) || "06".equals(split[1])){
                format=split[0]+"年二季度";
            }else if("07".equals(split[1]) || "08".equals(split[1]) || "09".equals(split[1])){
                format=split[0]+"年三季度";
            }else if("10".equals(split[1]) || "11".equals(split[1]) || "12".equals(split[1])){
                format=split[0]+"年四季度";
            }
//            Date parse = null;
//            try {
//                parse = sdf.parse(format);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }

            //上游原料信息插入
            List<BumawhRawBody> bumawhRawBodyList = bumawhHead.getBumawhRawBodyList();
            for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
                BumaStatisticsQuarter BumaStatisticsQuarter2 = new BumaStatisticsQuarter();
                BumaStatisticsQuarter2.setType("2");

                BumaStatisticsQuarter2.setDate(format);
                BumaStatisticsQuarter2.setManame(bumawhRawBody.getRawname());
                List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(BumaStatisticsQuarter2);
                if(!bumaStatisticsQuarters.isEmpty() && bumaStatisticsQuarters.size()>0){
                    BumaStatisticsQuarter statisticsQuarter = bumaStatisticsQuarters.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    int c = 0;
                    if("01".equals(split1[1]) || "02".equals(split1[1]) || "03".equals(split1[1])) c=1;
                    if("04".equals(split1[1]) || "05".equals(split1[1]) || "06".equals(split1[1])) c=4;
                    if("07".equals(split1[1]) || "08".equals(split1[1]) || "09".equals(split1[1])) c=7;
                    if("10".equals(split1[1]) || "11".equals(split1[1]) || "12".equals(split1[1])) c=10;
                    if(c!=0){
                        BigDecimal bigDecimal = new BigDecimal(0);
                        BigDecimal num = new BigDecimal(0);
                        for (int i = 1; i < 4; i++) {
                            String j = String.format("%02d", c);
                            c++;
                            format1 = split1[0]+"-"+j;
                            Map<String, String> pmap = new HashMap<>();
                            pmap.put("rawname",bumawhRawBody.getRawname());
                            pmap.put("sedate",format1);
                            BumawhRawBody bumawhRawBody1 = bumawhHeadMapper.selectAveRawPrice(pmap);
                            if(null != bumawhRawBody1 &&
                                    null != bumawhRawBody1.getMarketPrice() &&
                            bumawhRawBody1.getMarketPrice().compareTo(new BigDecimal(0))>0 &&
                                    null != bumawhRawBody1.getRatio() &&
                                    new BigDecimal(bumawhRawBody1.getRatio()).compareTo(new BigDecimal(0))>0
                            ){
                                bigDecimal = bigDecimal.add(bumawhRawBody1.getMarketPrice());
                                num = num.add(new BigDecimal(bumawhRawBody1.getRatio()));
                            }
                        }
                        if(num.compareTo(new BigDecimal(0))>0 &&
                        bigDecimal.compareTo(new BigDecimal(0))>0
                        ){
                            BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                            statisticsQuarter.setPrice(divide);
                        }
                        bumaStatisticsQuarterMapper.updateBumaStatisticsQuarter(statisticsQuarter);
                    }

                }
            }

            List<BumawhChaBody> bumawhChaBodyList = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
                BumaStatisticsQuarter BumaStatisticsQuarter2 = new BumaStatisticsQuarter();
                BumaStatisticsQuarter2.setType("3");
                BumaStatisticsQuarter2.setDate(format);
                BumaStatisticsQuarter2.setManame(bumawhChaBody.getChaname());
                List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(BumaStatisticsQuarter2);
                if (!bumaStatisticsQuarters.isEmpty() && bumaStatisticsQuarters.size() > 0) {
                    BumaStatisticsQuarter statisticsQuarter = bumaStatisticsQuarters.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    int c = 0;
                    if ("01".equals(split1[1]) || "02".equals(split1[1]) || "03".equals(split1[1])) c = 1;
                    if ("04".equals(split1[1]) || "05".equals(split1[1]) || "06".equals(split1[1])) c = 4;
                    if ("07".equals(split1[1]) || "08".equals(split1[1]) || "09".equals(split1[1])) c = 7;
                    if ("10".equals(split1[1]) || "11".equals(split1[1]) || "12".equals(split1[1])) c = 10;
                    if (c != 0) {
                        BigDecimal bigDecimal = new BigDecimal(0);
                        BigDecimal num = new BigDecimal(0);
                        for (int i = 1; i < 4; i++) {
                            String j = String.format("%02d", c);
                            c++;
                            format1 = split1[0] + "-" + j;
                            Map<String, String> pmap = new HashMap<>();
                            pmap.put("chaname", bumawhChaBody.getChaname());
                            pmap.put("sedate", format1);
                            BumawhChaBody bumawhChaBody1 = bumawhHeadMapper.selectAveChaPrice2(pmap);
                            if(null != bumawhChaBody1 &&
                                    null != bumawhChaBody1.getBuyAmount() &&
                                    null != bumawhChaBody1.getMarketPriceBb()
                            ){
                                bigDecimal = bigDecimal.add(bumawhChaBody1.getMarketPriceBb());
                                num = num.add(bumawhChaBody1.getBuyAmount());
                            }
                        }
                        if(num.compareTo(new BigDecimal(0))>0 &&
                                bigDecimal.compareTo(new BigDecimal(0))>0){
                            BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                            statisticsQuarter.setPrice(divide);
                        }
                        bumaStatisticsQuarterMapper.updateBumaStatisticsQuarter(statisticsQuarter);
                    }
                }
            }

            List<BumawhChaBody> bumawhChaBodyList2 = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList2) {
                BumaStatisticsQuarter BumaStatisticsQuarter2 = new BumaStatisticsQuarter();
                BumaStatisticsQuarter2.setType("1");
                BumaStatisticsQuarter2.setDate(format);
                BumaStatisticsQuarter2.setManame(bumawhChaBody.getChaname()+"采购价");
                BumaStatisticsQuarter2.setCode(bumawhChaBody.getChaname());
                List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(BumaStatisticsQuarter2);
                if(!bumaStatisticsQuarters.isEmpty() && bumaStatisticsQuarters.size()>0){
                    BumaStatisticsQuarter statisticsQuarter = bumaStatisticsQuarters.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    int c = 0;
                    if ("01".equals(split1[1]) || "02".equals(split1[1]) || "03".equals(split1[1])) c = 1;
                    if ("04".equals(split1[1]) || "05".equals(split1[1]) || "06".equals(split1[1])) c = 4;
                    if ("07".equals(split1[1]) || "08".equals(split1[1]) || "09".equals(split1[1])) c = 7;
                    if ("10".equals(split1[1]) || "11".equals(split1[1]) || "12".equals(split1[1])) c = 10;
                    if (c != 0) {
                        BigDecimal bigDecimal = new BigDecimal(0);
                        BigDecimal num = new BigDecimal(0);
                        for (int i = 1; i < 4; i++) {
                            String j = String.format("%02d", c);
                            c++;
                            format1 = split1[0] + "-" + j;
                            Map<String, String> pmap = new HashMap<>();
                            pmap.put("chaname", bumawhChaBody.getChaname());
                            pmap.put("sedate", format1);
                            BumawhChaBody bumawhChaBody1 = bumawhHeadMapper.selectAveChaPrice(pmap);
                            if (null != bumawhChaBody1 &&
                            null != bumawhChaBody1.getBuyPriceBb() &&
                                    null != bumawhChaBody1.getBuyAmount()
                            ){
                                bigDecimal = bigDecimal.add(bumawhChaBody1.getBuyPriceBb());
                                num = num.add(bumawhChaBody1.getBuyAmount());
                            }
                        }
                        if(num.compareTo(new BigDecimal(0))>0 &&
                                bigDecimal.compareTo(new BigDecimal(0))>0
                        ){
                            BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                            statisticsQuarter.setPrice(divide);
                        }
                        bumaStatisticsQuarterMapper.updateBumaStatisticsQuarter(statisticsQuarter);
                    }
                }
            }

//            List<BumawhErpmaBody> bumawhErpmaBodyList = bumawhHead.getBumawhErpmaBodyList();
            String code = bumawhHead.getCode();
            BumawhHead bumawhHead1 = bumawhHeadMapper.selectBumaHeadByCode(code);
            BumawhHead bumawhHead2 = bumawhHeadMapper.selectBumaHeadById2(String.valueOf(bumawhHead1.getId()));
            List<BumawhErpmaBody> bumawhErpmaBodyList1 = bumawhHead2.getBumawhErpmaBodyList();
            for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList1) {
                BumaStatisticsQuarter statisticsQuarter = new BumaStatisticsQuarter();
                statisticsQuarter.setType("4");
                statisticsQuarter.setDate(format);
                statisticsQuarter.setManame(bumawhErpmaBody.getErpname());
                statisticsQuarter.setCode(bumawhErpmaBody.getErpcode());
                List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(statisticsQuarter);
                if(!bumaStatisticsQuarters.isEmpty() && bumaStatisticsQuarters.size()>0){
                    BumaStatisticsQuarter statisticsQuarter1 = bumaStatisticsQuarters.get(0);
                    //平均价处理
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    String[] split1 = format1.split("-");
                    int c = 0;
                    if ("01".equals(split1[1]) || "02".equals(split1[1]) || "03".equals(split1[1])) c = 1;
                    if ("04".equals(split1[1]) || "05".equals(split1[1]) || "06".equals(split1[1])) c = 4;
                    if ("07".equals(split1[1]) || "08".equals(split1[1]) || "09".equals(split1[1])) c = 7;
                    if ("10".equals(split1[1]) || "11".equals(split1[1]) || "12".equals(split1[1])) c = 10;
                    if (c != 0) {
                        BigDecimal bigDecimal = new BigDecimal(0);
                        BigDecimal num = new BigDecimal(0);
                        for (int i = 1; i < 4; i++) {
                            String j = String.format("%02d", c);
                            c++;
                            format1 = split1[0] + "-" + j;
                            Bumapri bumapri1 = new Bumapri();
                            bumapri1.setManame(bumawhErpmaBody.getErpname());
                            bumapri1.setMacode(bumawhErpmaBody.getErpcode());
                            bumapri1.setPbilldate(format1);
                            Bumapri bumapri = bumapriMapper.selectBumaByMon(bumapri1);
                            if (null != bumapri &&
                                    null != bumapri.getNassistnum() &&
                                    null != bumapri.getNtaxmny()
                            ){
                                num = num.add(bumapri.getNassistnum());
                                bigDecimal = bigDecimal.add(bumapri.getNtaxmny());
                            }
                            //金额/数量
                            if(num.compareTo(new BigDecimal(0))>0 &&
                                    bigDecimal.compareTo(new BigDecimal(0))>0){
                                BigDecimal divide = bigDecimal.divide(num, 6, BigDecimal.ROUND_HALF_UP);
                                statisticsQuarter1.setPrice(divide);
                            }
                        }
                        bumaStatisticsQuarterMapper.updateBumaStatisticsQuarter(statisticsQuarter1);
                    }
                }
            }
        }
    }

    public void updateBumaStatisticsMonth(String id){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        BumawhHead bumawhHead = bumawhHeadMapper.selectBumawhHeadById2(id);
        if(null != bumawhHead){
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDay.setDate2(bumawhHead.getSedate());
//            bumaStatisticsDay.setManame(bumawhHead.getBomname());
//            List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(!bumaStatisticsDays.isEmpty() && bumaStatisticsDays.size()>0){
//                BumaStatisticsDay bumaStatisticsDay1 = bumaStatisticsDays.get(0);
//                bumaStatisticsDay1.setCode(bumawhHead.getCode());
//                bumaStatisticsDay1.setPrice(bumawhHead.getContractChaPrice());
//                bumaStatisticsDayMapper.updateBumaStatisticsDay(bumaStatisticsDay1);
//            }
            String format = sdf.format(bumawhHead.getSedate());
            Date parse = null;
            try {
                parse = sdf.parse(format);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            //上游原料信息插入
            List<BumawhRawBody> bumawhRawBodyList = bumawhHead.getBumawhRawBodyList();
            for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
                BumaStatisticsMonth BumaStatisticsMonth2 = new BumaStatisticsMonth();
                BumaStatisticsMonth2.setType("2");

                BumaStatisticsMonth2.setDate2(parse);
                BumaStatisticsMonth2.setManame(bumawhRawBody.getRawname());
                List<BumaStatisticsMonth> bumaStatisticsMonths = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(BumaStatisticsMonth2);
                if(!bumaStatisticsMonths.isEmpty() && bumaStatisticsMonths.size()>0){
                    BumaStatisticsMonth statisticsMonth = bumaStatisticsMonths.get(0);

                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    Map<String, String> pmap = new HashMap<>();
                    pmap.put("rawname",bumawhRawBody.getRawname());
                    pmap.put("sedate",format1);
                    BumawhRawBody bumawhRawBody1 = bumawhHeadMapper.selectAveRawPrice(pmap);
                    if(null !=bumawhRawBody1 &&
                            null != bumawhRawBody1.getMarketPrice() &&
                            null != bumawhRawBody1.getRatio() &&
                            (new BigDecimal(bumawhRawBody1.getRatio()).compareTo(new BigDecimal(0))>0)
                    ){
                        BigDecimal divide = bumawhRawBody1.getMarketPrice().divide(new BigDecimal(bumawhRawBody1.getRatio()), 6, BigDecimal.ROUND_HALF_UP);
                        statisticsMonth.setPrice(divide);
                    }
                    bumaStatisticsMonthMapper.updateBumaStatisticsMonth(statisticsMonth);
                }
            }

            List<BumawhChaBody> bumawhChaBodyList = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
                BumaStatisticsMonth BumaStatisticsMonth2 = new BumaStatisticsMonth();
                BumaStatisticsMonth2.setType("3");
                BumaStatisticsMonth2.setDate2(parse);
                BumaStatisticsMonth2.setManame(bumawhChaBody.getChaname());
                List<BumaStatisticsMonth> bumaStatisticsMonths = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(BumaStatisticsMonth2);
                if(!bumaStatisticsMonths.isEmpty() && bumaStatisticsMonths.size()>0){
                    BumaStatisticsMonth statisticsMonth = bumaStatisticsMonths.get(0);
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    Map<String, String> pmap = new HashMap<>();
                    pmap.put("chaname",bumawhChaBody.getChaname());
                    pmap.put("sedate",format1);
                    BumawhChaBody bumawhChaBody1 = bumawhHeadMapper.selectAveChaPrice2(pmap);
                    if(null != bumawhChaBody1 &&
                            null != bumawhChaBody1.getMarketPriceBb() &&
                            null != bumawhChaBody1.getBuyAmount() &&
                            (bumawhChaBody1.getBuyAmount().compareTo(new BigDecimal(0))>0) ){
                        BigDecimal divide = bumawhChaBody1.getMarketPriceBb().divide(bumawhChaBody1.getBuyAmount(), 6, BigDecimal.ROUND_HALF_UP);
                        statisticsMonth.setPrice(divide);
                        bumaStatisticsMonthMapper.updateBumaStatisticsMonth(statisticsMonth);
                    }
                }
            }

            List<BumawhChaBody> bumawhChaBodyList2 = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList2) {
                BumaStatisticsMonth BumaStatisticsMonth2 = new BumaStatisticsMonth();
                BumaStatisticsMonth2.setType("1");
                BumaStatisticsMonth2.setDate2(parse);
                BumaStatisticsMonth2.setManame(bumawhChaBody.getChaname()+"采购价");
                BumaStatisticsMonth2.setCode(bumawhChaBody.getChaname());
                List<BumaStatisticsMonth> bumaStatisticsMonths = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(BumaStatisticsMonth2);
                if(!bumaStatisticsMonths.isEmpty() && bumaStatisticsMonths.size()>0){
                    BumaStatisticsMonth statisticsMonth = bumaStatisticsMonths.get(0);
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    Map<String, String> pmap = new HashMap<>();
                    pmap.put("chaname",bumawhChaBody.getChaname());
                    pmap.put("sedate",format1);
                    BumawhChaBody bumawhChaBody1 = bumawhHeadMapper.selectAveChaPrice(pmap);
                    if(null != bumawhChaBody1 &&
                            null != bumawhChaBody1.getMarketPriceBb() &&
                            null != bumawhChaBody1.getBuyAmount() &&
                            (bumawhChaBody1.getBuyAmount().compareTo(new BigDecimal(0))>0) ){
                        BigDecimal divide = bumawhChaBody1.getBuyPriceBb().divide(bumawhChaBody1.getBuyAmount(), 6, BigDecimal.ROUND_HALF_UP);
                        statisticsMonth.setPrice(divide);
                        bumaStatisticsMonthMapper.updateBumaStatisticsMonth(statisticsMonth);
                    }
                }
            }

//            List<BumawhErpmaBody> bumawhErpmaBodyList = bumawhHead.getBumawhErpmaBodyList();
            String code = bumawhHead.getCode();
            BumawhHead bumawhHead1 = bumawhHeadMapper.selectBumaHeadByCode(code);
            BumawhHead bumawhHead2 = bumawhHeadMapper.selectBumaHeadById2(String.valueOf(bumawhHead1.getId()));
            List<BumawhErpmaBody> bumawhErpmaBodyList1 = bumawhHead2.getBumawhErpmaBodyList();
            for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList1) {
                BumaStatisticsMonth statisticsMonth = new BumaStatisticsMonth();
                statisticsMonth.setType("4");
                statisticsMonth.setDate2(parse);
                statisticsMonth.setManame(bumawhErpmaBody.getErpname());
                statisticsMonth.setCode(bumawhErpmaBody.getErpcode());
                List<BumaStatisticsMonth> bumaStatisticsMonths = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(statisticsMonth);
                if(!bumaStatisticsMonths.isEmpty() && bumaStatisticsMonths.size()>0){
                    BumaStatisticsMonth statisticsMonth1 = bumaStatisticsMonths.get(0);
//                    bumaStatisticsDay1.setCode(bumawhHead.getCode());
                    Bumapri bumapri1 = new Bumapri();
                    bumapri1.setManame(bumawhErpmaBody.getErpname());
                    bumapri1.setMacode(bumawhErpmaBody.getErpcode());
                    Date sedate = bumawhHead.getSedate();
                    String format1 = sdf.format(sedate);
                    bumapri1.setPbilldate(format1);
                    Bumapri bumapri = bumapriMapper.selectBumaByMon(bumapri1);
                    if(null != bumapri &&
                            null != bumapri.getNtaxmny() &&
                            null != bumapri.getNassistnum() &&
                            (bumapri.getNassistnum().compareTo(new BigDecimal(0))>0)
                            ){
                        BigDecimal nassistnum = bumapri.getNassistnum();
                        BigDecimal ntaxmny = bumapri.getNtaxmny();
                        //金额/数量
                        BigDecimal divide = ntaxmny.divide(nassistnum, 6, BigDecimal.ROUND_HALF_UP);
                        statisticsMonth1.setPrice(divide);
                    }
                    bumaStatisticsMonthMapper.updateBumaStatisticsMonth(statisticsMonth1);
                }
            }
        }
    }

    public void updateBumaStatisticsDay(String id){
        BumawhHead bumawhHead = bumawhHeadMapper.selectBumawhHeadById2(id);
        if(null != bumawhHead){
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDay.setDate2(bumawhHead.getSedate());
//            bumaStatisticsDay.setManame(bumawhHead.getBomname());
//            List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(!bumaStatisticsDays.isEmpty() && bumaStatisticsDays.size()>0){
//                BumaStatisticsDay bumaStatisticsDay1 = bumaStatisticsDays.get(0);
//                bumaStatisticsDay1.setCode(bumawhHead.getCode());
//                bumaStatisticsDay1.setPrice(bumawhHead.getContractChaPrice());
//                bumaStatisticsDayMapper.updateBumaStatisticsDay(bumaStatisticsDay1);
//            }

            List<BumawhRawBody> bumawhRawBodyList = bumawhHead.getBumawhRawBodyList();
            for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
                BumaStatisticsDay bumaStatisticsDay2 = new BumaStatisticsDay();
                bumaStatisticsDay2.setType("2");
                bumaStatisticsDay2.setDate2(bumawhHead.getSedate());
                bumaStatisticsDay2.setManame(bumawhRawBody.getRawname());
                List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
                if(!bumaStatisticsDays2.isEmpty() && bumaStatisticsDays2.size()>0){
                    BumaStatisticsDay bumaStatisticsDay1 = bumaStatisticsDays2.get(0);
//                    bumaStatisticsDay1.setCode(bumawhHead.getCode());
                    bumaStatisticsDay1.setPrice(bumawhRawBody.getMarketPrice());
                    bumaStatisticsDayMapper.updateBumaStatisticsDay(bumaStatisticsDay1);
                }
            }

            List<BumawhChaBody> bumawhChaBodyList = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
                BumaStatisticsDay bumaStatisticsDay2 = new BumaStatisticsDay();
                bumaStatisticsDay2.setType("3");
                bumaStatisticsDay2.setDate2(bumawhHead.getSedate());
                bumaStatisticsDay2.setManame(bumawhChaBody.getChaname());
                List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
                if(!bumaStatisticsDays2.isEmpty() && bumaStatisticsDays2.size()>0){
                    BumaStatisticsDay bumaStatisticsDay1 = bumaStatisticsDays2.get(0);
//                    bumaStatisticsDay1.setCode(bumawhHead.getCode());
                    bumaStatisticsDay1.setPrice(bumawhChaBody.getMarketPriceBb());
                    bumaStatisticsDayMapper.updateBumaStatisticsDay(bumaStatisticsDay1);
                }
            }

            List<BumawhChaBody> bumawhChaBodyList2 = bumawhHead.getBumawhChaBodyList();
            for (BumawhChaBody bumawhChaBody : bumawhChaBodyList2) {
                BumaStatisticsDay bumaStatisticsDay2 = new BumaStatisticsDay();
                bumaStatisticsDay2.setType("1");
                bumaStatisticsDay2.setDate2(bumawhHead.getSedate());
                bumaStatisticsDay2.setManame(bumawhChaBody.getChaname()+"采购价");
                bumaStatisticsDay2.setCode(bumawhChaBody.getChaname());
                List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
                if(!bumaStatisticsDays2.isEmpty() && bumaStatisticsDays2.size()>0){
                    BumaStatisticsDay bumaStatisticsDay1 = bumaStatisticsDays2.get(0);
//                    bumaStatisticsDay1.setCode(bumawhHead.getCode());
                    bumaStatisticsDay1.setPrice(bumawhChaBody.getBuyPriceBb());
                    bumaStatisticsDayMapper.updateBumaStatisticsDay(bumaStatisticsDay1);
                }
            }
        }
    }

    public AjaxResult bumawhaddinfo(String id){

        BumawhChaBody buch = new BumawhChaBody();
        buch.setBumaHeadId(Long.valueOf(id));
        BumawhHead bumawhHead = bumawhHeadMapper.selectBumawhHeadById2(id);

        String code = bumawhHead.getCode();
        Date sedate = bumawhHead.getSedate();
        BumawhHead bupram = new BumawhHead();
        bupram.setCode(code);
        BumawhHead bumaHead = bumawhHeadMapper.selectBumaHeadByCode(code);
        Long id1 = bumaHead.getId();
        BumawhErpmaBody bumawhErpmaBodyp = new BumawhErpmaBody();
        bumawhErpmaBodyp.setBumaHeadId(id1);
        List<BumawhErpmaBody> bumawhErpmaBodies = bumawhHeadMapper.selectBumawhErpmaBodyList(bumawhErpmaBodyp);
        //查询对应的erp物料


        return null;
    }

    public void bumawhaup(String id){
        //1.换算渠道价格
        BumawhChaBody buch = new BumawhChaBody();
        buch.setBumaHeadId(Long.valueOf(id));
        BumawhHead bumawhHead = bumawhHeadMapper.selectBumawhHeadById2(id);
        Date sedate = bumawhHead.getSedate();
        BurateItem bui = new BurateItem();
        bui.setRatedate(sedate);
        BurateItem bui2 = new BurateItem();
        List<BurateItem> burateItems = burateItemMapper.selectBurateItemList2(bui);
        List<BurateItem> burateItems2 = burateItemMapper.selectBurateItemList2(bui2);
        BurateItem burateItem = null;
        if(!burateItems.isEmpty() && burateItems.size()>0){
            burateItem = burateItems.get(0);
        }else if(!burateItems2.isEmpty() && burateItems2.size()>0){
            burateItem = burateItems2.get(0);
        }
        BumawhChaBody param = new BumawhChaBody();
        param.setBumaHeadId(Long.valueOf(id));
        List<BumawhChaBody> bumawhChaBodies = bumawhHeadMapper.selectBumawhChaBodyList(param);
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies) {
            if(burateItem != null){
                BigDecimal rate = burateItem.getRate();
                String currency = bumawhChaBody.getCurrency();
                BigDecimal marketPriceYb = bumawhChaBody.getMarketPriceYb();
                BigDecimal buyPriceYb = bumawhChaBody.getBuyPriceYb();
                if("美元".equals(currency)){
                    BigDecimal divide = marketPriceYb.multiply(rate);
                    bumawhChaBody.setMarketPriceBb(divide);
                    if(null != buyPriceYb) {
                        BigDecimal divide2 = buyPriceYb.multiply(rate);
                        bumawhChaBody.setBuyPriceBb(divide2);
                    }
                }else{
                    bumawhChaBody.setMarketPriceBb(marketPriceYb);
                    bumawhChaBody.setBuyPriceBb(buyPriceYb);
                }
                bumawhChaBody.setSedate(sedate);
                bumawhHeadMapper.updateBumawhChaBody(bumawhChaBody);
            }
        }
//        return null;
    }

    public AjaxResult avepri(String request){
        bumaAvepriMapper.deleteAll();
        JSONObject jsonObject = JSONObject.fromObject(request);
        String bomname = jsonObject.optString("bomname");
        String year = jsonObject.optString("year");
        String accyslep = jsonObject.optString("accysle");
        List<BumawhHead> bumawhHeads = bumawhHeadMapper.selectBumawhHeadByName(bomname);
        for (BumawhHead bumawhHead : bumawhHeads) {
            //获取时间和期间
            String accysle = bumawhHead.getAccysle();
//            List<BumawhErpmaBody> bumawhErpmaBodyList = bumawhHead.getBumawhErpmaBodyList();
//            for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList) {
//                Bumapri param = new Bumapri();
//                param.setManame(bumawhErpmaBody.getErpname());
//                param.setMacode(bumawhErpmaBody.getErpcode());
//                param.setPbilldate(year);
//                List<Bumapri> bumapris = bumapriMapper.selectBumaByName(param);

                //设置总金额相加 数量相加 计算均价
//                for (Bumapri bumapri : bumapris) {

                    BigDecimal num = new BigDecimal(0);
                    BigDecimal mon = new BigDecimal(0);
                    BigDecimal yf = new BigDecimal(0);
                    for (int i = 1; i <13 ; i++) {
                        String format = String.format("%02d", i);
                        if("月".equals(accysle)){
                            String s = i+"月";
                            if(!accyslep.equals(s)){
                                continue;
                            }
                        }else if("季度".equals(accysle)){
                            if(accyslep.equals("1季度") && !"01".equals(format) && !"02".equals(format) && !"03".equals(format)){
                                continue;
                            }else if(accyslep.equals("2季度") && !"04".equals(format) && !"05".equals(format) && !"06".equals(format)){
                                continue;
                            }else if(accyslep.equals("3季度") && !"07".equals(format) && !"08".equals(format) && !"09".equals(format)){
                                continue;
                            }else if(accyslep.equals("4季度") && !"10".equals(format) && !"11".equals(format) && !"12".equals(format)){
                                continue;
                            }
                        }else if("半年".equals(accysle)){
                            if(accyslep.equals("上半年") && !"01".equals(format) && !"02".equals(format) && !"03".equals(format) && !"04".equals(format) && !"05".equals(format) && !"06".equals(format)){
                                continue;
                            }else if(accyslep.equals("下半年") && !"07".equals(format) && !"08".equals(format) && !"09".equals(format) && !"10".equals(format) && !"11".equals(format) && !"12".equals(format)){
                                continue;
                            }
                        }
//                        param.setPbilldate(year+"-"+format);
//                        Bumapri bumapris1 = bumapriMapper.selectBumaByMon(param);

                        List<BumawhErpmaBody> bumawhErpmaBodyList2 = bumawhHead.getBumawhErpmaBodyList();
                        for (BumawhErpmaBody bumawhErpmaBody2 : bumawhErpmaBodyList2) {
                            Bumapri param2 = new Bumapri();
                            param2.setManame(bumawhErpmaBody2.getErpname());
                            param2.setMacode(bumawhErpmaBody2.getErpcode());
//                            param2.setPbilldate(year);
                            param2.setPbilldate(year+"-"+format);
                            Bumapri bumapris1 = bumapriMapper.selectBumaByMon(param2);

                            if(null != bumapris1){
                                //数量
                                num = num.add(bumapris1.getNassistnum());
                                //总金额
                                mon = mon.add(bumapris1.getNtaxmny());
                                //运费总额
                                if(null != bumapris1.getNmoney() && !"".equals(bumapris1.getNmoney())){
                                    yf = yf.add(bumapris1.getNmoney());
                                }
                                if(null != bumapris1.getVbdef21() && !"".equals(bumapris1.getVbdef21())){
                                    yf = yf.add(bumapris1.getVbdef21());
                                }
                            }
                        }

//                        if(null != bumapris1){
//                            //数量
//                            num = num.add(bumapris1.getNassistnum());
//                            //总金额
//                            mon = mon.add(bumapris1.getNtaxmny());
//                            //运费总额
//                            if(null != bumapris1.getNmoney() && !"".equals(bumapris1.getNmoney())){
//                                yf = yf.add(bumapris1.getNmoney());
//                            }
//                            if(null != bumapris1.getVbdef21() && !"".equals(bumapris1.getVbdef21())){
//                                yf = yf.add(bumapris1.getVbdef21());
//                            }
//                        }

                        if("月".equals(accysle)){
                            //设置
                            BumaAvepri bumaAvepri = new BumaAvepri();
                            bumaAvepri.setId(IdGen.uuid());
//                            bumaAvepri.setBomname(param.getManame());
                            bumaAvepri.setAccysle(i+"月");
                            bumaAvepri.setYear(Long.valueOf(year));
                            //入库总价 采购入库单明细行合计
                            bumaAvepri.setTotalPricePu(mon);
                            //入库数量
                            bumaAvepri.setAmount(num);

                            if(!num.equals(new BigDecimal(0))){
                                //入库均价(净价) 四舍五入保留两位小数
                                bumaAvepri.setAvePriceNet(mon.divide(num,2,BigDecimal.ROUND_HALF_UP));
                                //入库均价
                                bumaAvepri.setAvePrice(mon.add(yf).divide(num,2,BigDecimal.ROUND_HALF_UP));
                            }

                            //运费总金额 运费+杂项费
                            bumaAvepri.setTotalPriceTra(yf);
                            if(accyslep.equals(bumaAvepri.getAccysle())){
                                bumaAvepriMapper.insertBumaAvepri(bumaAvepri);
                            }
                            //数量
                            num = new BigDecimal(0);
                            //总金额
                            mon = new BigDecimal(0);
                            //运费总额
                            yf = new BigDecimal(0);
                        }else if("季度".equals(accysle) && ("03".equals(format) || "06".equals(format) || "09".equals(format) || "12".equals(format))){
                            BumaAvepri bumaAvepri = new BumaAvepri();
                            bumaAvepri.setId(IdGen.uuid());
                            if("03".equals(format)){
                                bumaAvepri.setAccysle("1季度");
                            }else if("06".equals(format)){
                                bumaAvepri.setAccysle("2季度");
                            }else if("09".equals(format)){
                                bumaAvepri.setAccysle("3季度");
                            }else if("12".equals(format)){
                                bumaAvepri.setAccysle("4季度");
                            }
//                            bumaAvepri.setBomname(param.getManame());
                            bumaAvepri.setYear(Long.valueOf(year));
                            //入库总价 采购入库单明细行合计
                            bumaAvepri.setTotalPricePu(mon);
                            //入库数量
                            bumaAvepri.setAmount(num);
                            if(!num.equals(new BigDecimal(0))){
                                //入库均价(净价) 四舍五入保留两位小数
                                bumaAvepri.setAvePriceNet(mon.divide(num,2,BigDecimal.ROUND_HALF_UP));
                                //入库均价
                                bumaAvepri.setAvePrice(mon.add(yf).divide(num,2,BigDecimal.ROUND_HALF_UP));
                            }
                            //运费总金额 运费+杂项费
                            bumaAvepri.setTotalPriceTra(yf);
                            if(accyslep.equals(bumaAvepri.getAccysle())){
                                bumaAvepriMapper.insertBumaAvepri(bumaAvepri);
                            }
                            //数量
                            num = new BigDecimal(0);
                            //总金额
                            mon = new BigDecimal(0);
                            //运费总额
                            yf = new BigDecimal(0);
                        }else if("半年".equals(accysle) && ("06".equals(format) || "12".equals(format)) ){
                            BumaAvepri bumaAvepri = new BumaAvepri();
                            if("06".equals(format)){
                                bumaAvepri.setAccysle("上半年");
                            }else if("12".equals(format)){
                                bumaAvepri.setAccysle("下半年");
                            }
                            bumaAvepri.setId(IdGen.uuid());
//                            bumaAvepri.setBomname(param.getManame());
                            bumaAvepri.setYear(Long.valueOf(year));
                            //入库总价 采购入库单明细行合计
                            bumaAvepri.setTotalPricePu(mon);
                            //入库数量
                            bumaAvepri.setAmount(num);
                            if(!num.equals(new BigDecimal(0))){
                                //入库均价(净价) 四舍五入保留两位小数
                                bumaAvepri.setAvePriceNet(mon.divide(num,2,BigDecimal.ROUND_HALF_UP));
                                //入库均价
                                bumaAvepri.setAvePrice(mon.add(yf).divide(num,2,BigDecimal.ROUND_HALF_UP));
                            }
                            //运费总金额 运费+杂项费
                            bumaAvepri.setTotalPriceTra(yf);
                            if(accyslep.equals(bumaAvepri.getAccysle())){
                                bumaAvepriMapper.insertBumaAvepri(bumaAvepri);
                            }
                            //数量
                            num = new BigDecimal(0);
                            //总金额
                            mon = new BigDecimal(0);
                            //运费总额
                            yf = new BigDecimal(0);
                    }else if("年".equals(accysle) && "12".equals(format)){
                            BumaAvepri bumaAvepri = new BumaAvepri();
                            bumaAvepri.setId(IdGen.uuid());
                            bumaAvepri.setAccysle("年");
//                            bumaAvepri.setBomname(param.getManame());
                            bumaAvepri.setYear(Long.valueOf(year));
                            //入库总价 采购入库单明细行合计
                            bumaAvepri.setTotalPricePu(mon);
                            //入库数量
                            bumaAvepri.setAmount(num);
                            if(!num.equals(new BigDecimal(0))){
                                //入库均价(净价) 四舍五入保留两位小数
                                bumaAvepri.setAvePriceNet(mon.divide(num,2,BigDecimal.ROUND_HALF_UP));
                                //入库均价
                                bumaAvepri.setAvePrice(mon.add(yf).divide(num,2,BigDecimal.ROUND_HALF_UP));
                            }
                            //运费总金额 运费+杂项费
                            bumaAvepri.setTotalPriceTra(yf);
                            if(accyslep.equals(bumaAvepri.getAccysle())){
                                bumaAvepriMapper.insertBumaAvepri(bumaAvepri);
                            }
                    }

                    }
//                }

//            }

        }

        return null;
    }
    public void statisticinitday(String request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = sdf.format(date);
        if(StringUtils.isNotEmpty(request)){
            JSONObject jsonObject = JSONObject.fromObject(request);
            String date1 = jsonObject.optString("date");
            if(StringUtils.isNotEmpty(date1)){
                try {
                    Date parse = sdf.parse(date1);
                    date = parse;
                    format=date1;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

        }
        //1. 新增当天所有物料信息 物料大类合同价 原料价 渠道报价

//        定时任务根据对照表新增 物料大类
//        List<BumawhHead> bumawhHeads = bumawhHeadMapper.selectBumaHeadBomName();
//        for (BumawhHead bumawhHead : bumawhHeads) {
//            String bomname = bumawhHead.getBomname();
//            String code = bumawhHead.getCode();
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setManame(bomname);
//            bumaStatisticsDay.setCode(code);
//            bumaStatisticsDay.setDate(format);
//            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//        }



        //        定时任务根据对照表新增 原料
        List<BumawhRawBody> bumawhRawBodies = bumawhHeadMapper.selectBumaRawBody();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodies) {
            String rawname = bumawhRawBody.getRawname();
            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
            bumaStatisticsDay.setManame(rawname);
            bumaStatisticsDay.setDate(format);
            bumaStatisticsDay.setDate2(date);
            bumaStatisticsDay.setType("2");
            bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
        }

        //        定时任务根据对照表新增 渠道
        List<BumawhChaBody> bumawhChaBodies = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
            bumaStatisticsDay.setManame(chaname);
            bumaStatisticsDay.setDate(format);
            bumaStatisticsDay.setDate2(date);
            bumaStatisticsDay.setType("3");
            bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
        }
        //        定时任务根据对照表新增 渠道采购价--替换物料大类
        List<BumawhChaBody> bumawhChaBodies2 = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBodie : bumawhChaBodies2) {
            String chaname = bumawhChaBodie.getChaname();
            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
            bumaStatisticsDay.setManame(chaname+"采购价");
            bumaStatisticsDay.setCode(chaname);
            bumaStatisticsDay.setDate(format);
            bumaStatisticsDay.setDate2(date);
            bumaStatisticsDay.setType("1");
            bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
        }


//        根据对照表新增之后可以根据价格维护查询有则修改,
//        无则新增
        //新增当天物料大类的价格
        //
        //2. 查询是否有遗漏的物料
        //3. 有遗漏物料重头补到尾
    }

    public void statisticinitmonth(String request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        String format = sdf.format(date);
        if(StringUtils.isNotEmpty(request)){
            JSONObject jsonObject = JSONObject.fromObject(request);
            String date1 = jsonObject.optString("date");
            if(StringUtils.isNotEmpty(date1)){
                try {
                    Date parse = sdf.parse(date1);
                    String format1 = sdf.format(parse);
                    date = parse;
                    format=format1;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

        }

        //1. 新增当天所有物料信息 物料大类合同价 原料价 渠道报价

//        定时任务根据对照表新增 物料大类
//        List<BumawhHead> bumawhHeads = bumawhHeadMapper.selectBumaHeadBomName();
//        for (BumawhHead bumawhHead : bumawhHeads) {
//            String bomname = bumawhHead.getBomname();
//            String code = bumawhHead.getCode();
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setManame(bomname);
//            bumaStatisticsDay.setCode(code);
//            bumaStatisticsDay.setDate(format);
//            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//        }

        //        定时任务根据对照表新增 原料
        List<BumawhRawBody> bumawhRawBodies = bumawhHeadMapper.selectBumaRawBody();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodies) {
            String rawname = bumawhRawBody.getRawname();
            BumaStatisticsMonth bumaStatisticsMonth = new BumaStatisticsMonth();
            bumaStatisticsMonth.setManame(rawname);
            bumaStatisticsMonth.setDate(format);
            bumaStatisticsMonth.setDate2(date);
            bumaStatisticsMonth.setType("2");
            bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
        }

        //        定时任务根据对照表新增 渠道
        List<BumawhChaBody> bumawhChaBodies = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsMonth bumaStatisticsMonth = new BumaStatisticsMonth();
            bumaStatisticsMonth.setManame(chaname);
            bumaStatisticsMonth.setDate(format);
            bumaStatisticsMonth.setDate2(date);
            bumaStatisticsMonth.setType("3");
            bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
        }
        //        定时任务根据对照表新增 渠道采购价
        List<BumawhChaBody> bumawhChaBodies2 = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies2) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsMonth bumaStatisticsMonth = new BumaStatisticsMonth();
            bumaStatisticsMonth.setManame(chaname+"采购价");
            bumaStatisticsMonth.setCode(chaname);
            bumaStatisticsMonth.setDate(format);
            bumaStatisticsMonth.setDate2(date);
            bumaStatisticsMonth.setType("1");
            bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
        }
        // erp物料
        List<BumawhErpmaBody> bumawhErpmaBodies = bumawhHeadMapper.selectBumaErpmaBody();
        for (BumawhErpmaBody bumawhChaBody : bumawhErpmaBodies) {
            String erpname = bumawhChaBody.getErpname();
            String erpcode = bumawhChaBody.getErpcode();
            BumaStatisticsMonth bumaStatisticsMonth = new BumaStatisticsMonth();
            bumaStatisticsMonth.setManame(erpname);
            bumaStatisticsMonth.setCode(erpcode);
            bumaStatisticsMonth.setDate(format);
            bumaStatisticsMonth.setDate2(date);
            bumaStatisticsMonth.setType("4");
            bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
        }
    }

    public void statisticinitquarter(String request){

        //处理时间
        //定时任务时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        String format = sdf.format(date);
        //手动调用时间
        if(StringUtils.isNotEmpty(request)){
            JSONObject jsonObject = JSONObject.fromObject(request);
            String date1 = jsonObject.optString("date");
            if(StringUtils.isNotEmpty(date1)){
                try {
                    Date parse = sdf.parse(date1);
                    String format1 = sdf.format(parse);
                    date = parse;
                    format=format1;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        //处理时间为季度格式
        String[] split = format.split("-");
        if("01".equals(split[1])){
            format=split[0]+"年一季度";
        }else if("04".equals(split[1])){
            format=split[0]+"年二季度";
        }else if("07".equals(split[1])){
            format=split[0]+"年三季度";
        }else if("10".equals(split[1])){
            format=split[0]+"年四季度";
        }
        //1. 新增当天所有物料信息 物料大类合同价 原料价 渠道报价

//        定时任务根据对照表新增 物料大类
//        List<BumawhHead> bumawhHeads = bumawhHeadMapper.selectBumaHeadBomName();
//        for (BumawhHead bumawhHead : bumawhHeads) {
//            String bomname = bumawhHead.getBomname();
//            String code = bumawhHead.getCode();
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setManame(bomname);
//            bumaStatisticsDay.setCode(code);
//            bumaStatisticsDay.setDate(format);
//            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//        }

        //        定时任务根据对照表新增 原料
        List<BumawhRawBody> bumawhRawBodies = bumawhHeadMapper.selectBumaRawBody();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodies) {
            String rawname = bumawhRawBody.getRawname();
            BumaStatisticsQuarter bumaStatisticsQuarter = new BumaStatisticsQuarter();
            bumaStatisticsQuarter.setManame(rawname);
            bumaStatisticsQuarter.setDate(format);
            bumaStatisticsQuarter.setDate2(date);
            bumaStatisticsQuarter.setType("2");
            bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
        }

        //        定时任务根据对照表新增 渠道
        List<BumawhChaBody> bumawhChaBodies = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsQuarter bumaStatisticsQuarter = new BumaStatisticsQuarter();
            bumaStatisticsQuarter.setManame(chaname);
            bumaStatisticsQuarter.setDate(format);
            bumaStatisticsQuarter.setDate2(date);
            bumaStatisticsQuarter.setType("3");
            bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
        }
        //        定时任务根据对照表新增 渠道
        List<BumawhChaBody> bumawhChaBodies2 = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies2) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsQuarter bumaStatisticsQuarter = new BumaStatisticsQuarter();
            bumaStatisticsQuarter.setManame(chaname+"采购价");
            bumaStatisticsQuarter.setCode(chaname);
            bumaStatisticsQuarter.setDate(format);
            bumaStatisticsQuarter.setDate2(date);
            bumaStatisticsQuarter.setType("1");
            bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
        }
        // erp物料
        List<BumawhErpmaBody> bumawhErpmaBodies = bumawhHeadMapper.selectBumaErpmaBody();
        for (BumawhErpmaBody bumawhChaBody : bumawhErpmaBodies) {
            String erpname = bumawhChaBody.getErpname();
            String erpcode = bumawhChaBody.getErpcode();
            BumaStatisticsQuarter bumaStatisticsQuarter = new BumaStatisticsQuarter();
            bumaStatisticsQuarter.setManame(erpname);
            bumaStatisticsQuarter.setCode(erpcode);
            bumaStatisticsQuarter.setDate(format);
            bumaStatisticsQuarter.setDate2(date);
            bumaStatisticsQuarter.setType("4");
            bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
        }
    }

    public void statisticinithalfyear(String request){

        //处理时间
        //定时任务时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        String format = sdf.format(date);
        //手动调用时间
        if(StringUtils.isNotEmpty(request)){
            JSONObject jsonObject = JSONObject.fromObject(request);
            String date1 = jsonObject.optString("date");
            if(StringUtils.isNotEmpty(date1)){
                try {
                    Date parse = sdf.parse(date1);
                    String format1 = sdf.format(parse);
                    date = parse;
                    format=format1;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        //处理时间为季度格式
        String[] split = format.split("-");
        if("01".equals(split[1])){
            format=split[0]+"年上半年";
        }else if("07".equals(split[1])){
            format=split[0]+"年下半年";
        }
        //1. 新增当天所有物料信息 物料大类合同价 原料价 渠道报价

//        定时任务根据对照表新增 物料大类
//        List<BumawhHead> bumawhHeads = bumawhHeadMapper.selectBumaHeadBomName();
//        for (BumawhHead bumawhHead : bumawhHeads) {
//            String bomname = bumawhHead.getBomname();
//            String code = bumawhHead.getCode();
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setManame(bomname);
//            bumaStatisticsDay.setCode(code);
//            bumaStatisticsDay.setDate(format);
//            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//        }

        //        定时任务根据对照表新增 原料
        List<BumawhRawBody> bumawhRawBodies = bumawhHeadMapper.selectBumaRawBody();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodies) {
            String rawname = bumawhRawBody.getRawname();
            BumaStatisticsHalfyear bumaStatisticsHalfyear = new BumaStatisticsHalfyear();
            bumaStatisticsHalfyear.setManame(rawname);
            bumaStatisticsHalfyear.setDate(format);
            bumaStatisticsHalfyear.setDate2(date);
            bumaStatisticsHalfyear.setType("2");
            bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
        }

        //        定时任务根据对照表新增 渠道
        List<BumawhChaBody> bumawhChaBodies = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsHalfyear bumaStatisticsHalfyear = new BumaStatisticsHalfyear();
            bumaStatisticsHalfyear.setManame(chaname);
            bumaStatisticsHalfyear.setDate(format);
            bumaStatisticsHalfyear.setDate2(date);
            bumaStatisticsHalfyear.setType("3");
            bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
        }
        //        定时任务根据对照表新增 渠道
        List<BumawhChaBody> bumawhChaBodies2 = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies2) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsHalfyear bumaStatisticsHalfyear = new BumaStatisticsHalfyear();
            bumaStatisticsHalfyear.setManame(chaname+"采购价");
            bumaStatisticsHalfyear.setCode(chaname);
            bumaStatisticsHalfyear.setDate(format);
            bumaStatisticsHalfyear.setDate2(date);
            bumaStatisticsHalfyear.setType("1");
            bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
        }
        // erp物料
        List<BumawhErpmaBody> bumawhErpmaBodies = bumawhHeadMapper.selectBumaErpmaBody();
        for (BumawhErpmaBody bumawhChaBody : bumawhErpmaBodies) {
            String erpname = bumawhChaBody.getErpname();
            String erpcode = bumawhChaBody.getErpcode();
            BumaStatisticsHalfyear bumaStatisticsHalfyear = new BumaStatisticsHalfyear();
            bumaStatisticsHalfyear.setManame(erpname);
            bumaStatisticsHalfyear.setCode(erpcode);
            bumaStatisticsHalfyear.setDate(format);
            bumaStatisticsHalfyear.setDate2(date);
            bumaStatisticsHalfyear.setType("4");
            bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
        }
    }
    public void statisticinityear(String request){

        //处理时间
        //定时任务时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        String format = sdf.format(date);
        //手动调用时间
        if(StringUtils.isNotEmpty(request)){
            JSONObject jsonObject = JSONObject.fromObject(request);
            String date1 = jsonObject.optString("date");
            if(StringUtils.isNotEmpty(date1)){
                try {
                    Date parse = sdf.parse(date1);
                    String format1 = sdf.format(parse);
                    date = parse;
                    format=format1;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        //处理时间为季度格式
        String[] split = format.split("-");
        if("01".equals(split[1])){
            format=split[0]+"年";
        }
        //1. 新增当天所有物料信息 物料大类合同价 原料价 渠道报价

//        定时任务根据对照表新增 物料大类
//        List<BumawhHead> bumawhHeads = bumawhHeadMapper.selectBumaHeadBomName();
//        for (BumawhHead bumawhHead : bumawhHeads) {
//            String bomname = bumawhHead.getBomname();
//            String code = bumawhHead.getCode();
//            BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//            bumaStatisticsDay.setManame(bomname);
//            bumaStatisticsDay.setCode(code);
//            bumaStatisticsDay.setDate(format);
//            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setType("1");
//            bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//        }

        //        定时任务根据对照表新增 原料
        List<BumawhRawBody> bumawhRawBodies = bumawhHeadMapper.selectBumaRawBody();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodies) {
            String rawname = bumawhRawBody.getRawname();
            BumaStatisticsYear bumaStatisticsYear = new BumaStatisticsYear();
            bumaStatisticsYear.setManame(rawname);
            bumaStatisticsYear.setDate(format);
            bumaStatisticsYear.setDate2(date);
            bumaStatisticsYear.setType("2");
            bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
        }

        //        定时任务根据对照表新增 渠道
        List<BumawhChaBody> bumawhChaBodies = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsYear bumaStatisticsYear = new BumaStatisticsYear();
            bumaStatisticsYear.setManame(chaname);
            bumaStatisticsYear.setDate(format);
            bumaStatisticsYear.setDate2(date);
            bumaStatisticsYear.setType("3");
            bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
        }
        //        定时任务根据对照表新增 渠道
        List<BumawhChaBody> bumawhChaBodies2 = bumawhHeadMapper.selectBumaChaBody();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodies2) {
            String chaname = bumawhChaBody.getChaname();
            BumaStatisticsYear bumaStatisticsYear = new BumaStatisticsYear();
            bumaStatisticsYear.setManame(chaname+"采购价");
            bumaStatisticsYear.setCode(chaname);
            bumaStatisticsYear.setDate(format);
            bumaStatisticsYear.setDate2(date);
            bumaStatisticsYear.setType("1");
            bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
        }
        // erp物料
        List<BumawhErpmaBody> bumawhErpmaBodies = bumawhHeadMapper.selectBumaErpmaBody();
        for (BumawhErpmaBody bumawhChaBody : bumawhErpmaBodies) {
            String erpname = bumawhChaBody.getErpname();
            String erpcode = bumawhChaBody.getErpcode();
            BumaStatisticsYear bumaStatisticsYear = new BumaStatisticsYear();
            bumaStatisticsYear.setManame(erpname);
            bumaStatisticsYear.setCode(erpcode);
            bumaStatisticsYear.setDate(format);
            bumaStatisticsYear.setDate2(date);
            bumaStatisticsYear.setType("4");
            bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
        }
    }
    /**
     * @author: Liujiaji
     * @date: 2024/7/1 16:59
     * @description: TODO 折线图数据处理定时任务
     * @param: []
     * @return: void
     **/
    public void taskstatistics(String request){



        String date1 ="";
        if(StringUtils.isNotEmpty(request)){
            JSONObject jsonObject = JSONObject.fromObject(request);
            date1 = jsonObject.optString("date");
        }
        //日表维护
        statisticinitday(request);

        //月表维护
        Calendar instance = Calendar.getInstance();
        int di = instance.get(Calendar.DAY_OF_MONTH);
        int mi1 = instance.get(Calendar.MONTH)+1;

        String[] split = date1.split("-");
        //判断是否是每月1号,一月维护一季度,和上半年,年,4,10月维护二四季度,7维护三季度和下半年
        if(di==1 || "01".equals(split[2])){
            //月维护
            statisticinitmonth(request);
            //1季度 上半年 年 维护 1月
            if(mi1==1 || "01".equals(split[1])){
                statisticinitquarter(request);
                statisticinithalfyear(request);
                statisticinityear(request);
            }
            //24季度维护 4 10 月
            if(mi1==4 || "04".equals(split[1]) || mi1==10 || "10".equals(split[1])){
                statisticinitquarter(request);
            }
            //3季度 下半年维护 7月
            if(mi1==7 || "07".equals(split[1])){
                statisticinitquarter(request);
                statisticinithalfyear(request);
            }
        }


    }

    public void handBumaStatisticsDay(String request){
        JSONObject jsonObject = JSONObject.fromObject(request);
        //查询信息
        String id = jsonObject.optString("id");

        BumawhHead bumaHead = bumawhHeadMapper.selectBumaHeadById2(id);
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -1);
//        Date date1 = calendar.getTime();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
//        BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//        bumaStatisticsDay.setManame(bumaHead.getBomname());
//        bumaStatisticsDay.setType("1");
////        bumaStatisticsDay.setDate2(date);
//        List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//        if(bumaStatisticsDays.isEmpty() || bumaStatisticsDays.size()<1){
//            BumaStatisticsDay param = new BumaStatisticsDay();
//            List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
//            for (BumaStatisticsDay statisticsDay : bumaStatisticsDays1) {
//                bumaStatisticsDay.setDate2(statisticsDay.getDate2());
//                bumaStatisticsDay.setDate(statisticsDay.getDate());
//                bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//            }
//        }else{
////            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setDate(format);
//            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(bumaStatisticsDays2.isEmpty()){
//                bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//            }
//        }


        List<BumawhRawBody> bumawhRawBodyList = bumaHead.getBumawhRawBodyList();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
            BumaStatisticsDay bumaStatisticsDay2 = new BumaStatisticsDay();
            bumaStatisticsDay2.setManame(bumawhRawBody.getRawname());
            bumaStatisticsDay2.setType("2");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
            if(bumaStatisticsDays2.isEmpty() || bumaStatisticsDays2.size()<1){
                BumaStatisticsDay param = new BumaStatisticsDay();
                List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
                for (BumaStatisticsDay statisticsDay : bumaStatisticsDays1) {
                    bumaStatisticsDay2.setDate2(statisticsDay.getDate2());
                    bumaStatisticsDay2.setDate(statisticsDay.getDate());
                    bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay2);
                }
            } else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsDay2.setDate(format);
                List<BumaStatisticsDay> bumaStatisticsDays22 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
                if(bumaStatisticsDays22.isEmpty()){
                    bumaStatisticsDay2.setDate2(date);
                    bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay2);
                }
            }
        }

        List<BumawhChaBody> bumawhChaBodyList = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
            BumaStatisticsDay bumaStatisticsDay2 = new BumaStatisticsDay();
            bumaStatisticsDay2.setManame(bumawhChaBody.getChaname());
            bumaStatisticsDay2.setType("3");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
            if(bumaStatisticsDays2.isEmpty() || bumaStatisticsDays2.size()<1){
                BumaStatisticsDay param = new BumaStatisticsDay();
                List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
                for (BumaStatisticsDay statisticsDay : bumaStatisticsDays1) {
                    bumaStatisticsDay2.setDate2(statisticsDay.getDate2());
                    bumaStatisticsDay2.setDate(statisticsDay.getDate());
                    bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay2);
                }
            } else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsDay2.setDate(format);
                List<BumaStatisticsDay> bumaStatisticsDays22 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
                if(bumaStatisticsDays22.isEmpty()){
                    bumaStatisticsDay2.setDate2(date);
                    bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay2);
                }
            }
        }
        //渠道采购价维护
        List<BumawhChaBody> bumawhChaBodyList2 = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList2) {
            BumaStatisticsDay bumaStatisticsDay2 = new BumaStatisticsDay();
            bumaStatisticsDay2.setManame(bumawhChaBody.getChaname()+"采购价");
            bumaStatisticsDay2.setCode(bumawhChaBody.getChaname());
            bumaStatisticsDay2.setType("1");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
            if(bumaStatisticsDays2.isEmpty() || bumaStatisticsDays2.size()<1){
                BumaStatisticsDay param = new BumaStatisticsDay();
                List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
                for (BumaStatisticsDay statisticsDay : bumaStatisticsDays1) {
                    bumaStatisticsDay2.setDate2(statisticsDay.getDate2());
                    bumaStatisticsDay2.setDate(statisticsDay.getDate());
                    bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay2);
                }
            } else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsDay2.setDate(format);
                List<BumaStatisticsDay> bumaStatisticsDays22 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
                if(bumaStatisticsDays22.isEmpty()){
                    bumaStatisticsDay2.setDate2(date);
                    bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay2);
                }
            }
        }
    }

    public void handBumaStatisticsMonth(String request){
        JSONObject jsonObject = JSONObject.fromObject(request);
        //查询信息
        String id = jsonObject.optString("id");

        BumawhHead bumaHead = bumawhHeadMapper.selectBumaHeadById2(id);
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -1);
//        Date date1 = calendar.getTime();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(date);
        Date parse = null;
        try {
            parse = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//        bumaStatisticsDay.setManame(bumaHead.getBomname());
//        bumaStatisticsDay.setType("1");
////        bumaStatisticsDay.setDate2(date);
//        List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//        if(bumaStatisticsDays.isEmpty() || bumaStatisticsDays.size()<1){
//            BumaStatisticsDay param = new BumaStatisticsDay();
//            List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
//            for (BumaStatisticsDay statisticsDay : bumaStatisticsDays1) {
//                bumaStatisticsDay.setDate2(statisticsDay.getDate2());
//                bumaStatisticsDay.setDate(statisticsDay.getDate());
//                bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//            }
//        }else{
////            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setDate(format);
//            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(bumaStatisticsDays2.isEmpty()){
//                bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//            }
//        }



        List<BumawhRawBody> bumawhRawBodyList = bumaHead.getBumawhRawBodyList();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
            BumaStatisticsMonth bumaStatisticsMonth = new BumaStatisticsMonth();
            bumaStatisticsMonth.setManame(bumawhRawBody.getRawname());
            bumaStatisticsMonth.setType("2");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsMonth> bumaStatisticsMonths = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(bumaStatisticsMonth);
//            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
            if(bumaStatisticsMonths.isEmpty() || bumaStatisticsMonths.size()<1){
                BumaStatisticsMonth param = new BumaStatisticsMonth();
                List<BumaStatisticsMonth> selectdate = bumaStatisticsMonthMapper.selectdate(param);
//                List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
                for (BumaStatisticsMonth statisticsMonth : selectdate) {
                    bumaStatisticsMonth.setDate2(statisticsMonth.getDate2());
                    bumaStatisticsMonth.setDate(statisticsMonth.getDate());
                    bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
                }
            } else{
                bumaStatisticsMonth.setDate(format);
                List<BumaStatisticsMonth> bumaStatisticsMonths1 = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(bumaStatisticsMonth);
                if(bumaStatisticsMonths1.isEmpty()){
                    bumaStatisticsMonth.setDate2(parse);
                    bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
                }
            }
        }

        List<BumawhChaBody> bumawhChaBodyList = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
            BumaStatisticsMonth bumaStatisticsMonth = new BumaStatisticsMonth();
            bumaStatisticsMonth.setManame(bumawhChaBody.getChaname());
            bumaStatisticsMonth.setType("3");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsMonth> bumaStatisticsMonths = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(bumaStatisticsMonth);
            if(bumaStatisticsMonths.isEmpty() || bumaStatisticsMonths.size()<1){
                BumaStatisticsMonth param = new BumaStatisticsMonth();
                List<BumaStatisticsMonth> selectdate = bumaStatisticsMonthMapper.selectdate(param);
                for (BumaStatisticsMonth statisticsMonth : selectdate) {
                    bumaStatisticsMonth.setDate2(statisticsMonth.getDate2());
                    bumaStatisticsMonth.setDate(statisticsMonth.getDate());
                    bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsMonth.setDate(format);
                List<BumaStatisticsMonth> bumaStatisticsMonths1 = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(bumaStatisticsMonth);
                if(bumaStatisticsMonths1.isEmpty()){
                    bumaStatisticsMonth.setDate2(parse);
                    bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
                }
            }
        }
        //渠道采购价维护
        List<BumawhChaBody> bumawhChaBodyList1 = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList1) {
            BumaStatisticsMonth bumaStatisticsMonth = new BumaStatisticsMonth();
            bumaStatisticsMonth.setManame(bumawhChaBody.getChaname()+"采购价");
            bumaStatisticsMonth.setCode(bumawhChaBody.getChaname());
            bumaStatisticsMonth.setType("1");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsMonth> bumaStatisticsMonths = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(bumaStatisticsMonth);
            if(bumaStatisticsMonths.isEmpty() || bumaStatisticsMonths.size()<1){
                BumaStatisticsMonth param = new BumaStatisticsMonth();
                List<BumaStatisticsMonth> selectdate = bumaStatisticsMonthMapper.selectdate(param);
                for (BumaStatisticsMonth statisticsMonth : selectdate) {
                    bumaStatisticsMonth.setDate2(statisticsMonth.getDate2());
                    bumaStatisticsMonth.setDate(statisticsMonth.getDate());
                    bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsMonth.setDate(format);
                List<BumaStatisticsMonth> bumaStatisticsMonths1 = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(bumaStatisticsMonth);
                if(bumaStatisticsMonths1.isEmpty()){
                    bumaStatisticsMonth.setDate2(parse);
                    bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
                }
            }
        }
        //ERP价维护
        List<BumawhErpmaBody> bumawhErpmaBodyList = bumaHead.getBumawhErpmaBodyList();
        for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList) {
            BumaStatisticsMonth bumaStatisticsMonth = new BumaStatisticsMonth();
            bumaStatisticsMonth.setManame(bumawhErpmaBody.getErpname());
            bumaStatisticsMonth.setCode(bumawhErpmaBody.getErpcode());
            bumaStatisticsMonth.setType("4");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsMonth> bumaStatisticsMonths = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(bumaStatisticsMonth);
            if(bumaStatisticsMonths.isEmpty() || bumaStatisticsMonths.size()<1){
                BumaStatisticsMonth param = new BumaStatisticsMonth();
                List<BumaStatisticsMonth> selectdate = bumaStatisticsMonthMapper.selectdate(param);
                for (BumaStatisticsMonth statisticsMonth : selectdate) {
                    bumaStatisticsMonth.setDate2(statisticsMonth.getDate2());
                    bumaStatisticsMonth.setDate(statisticsMonth.getDate());
                    bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsMonth.setDate(format);
                List<BumaStatisticsMonth> bumaStatisticsMonths1 = bumaStatisticsMonthMapper.selectBumaStatisticsMonthList(bumaStatisticsMonth);
                if(bumaStatisticsMonths1.isEmpty()){
                    bumaStatisticsMonth.setDate2(parse);
                    bumaStatisticsMonthMapper.insertBumaStatisticsMonth(bumaStatisticsMonth);
                }
            }
        }
    }

    public void handBumaStatisticsQuarter(String request){
        JSONObject jsonObject = JSONObject.fromObject(request);
        //查询信息
        String id = jsonObject.optString("id");

        BumawhHead bumaHead = bumawhHeadMapper.selectBumaHeadById2(id);
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -1);
//        Date date1 = calendar.getTime();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(date);
        String bzformat = "";
        //格式时间
        String[] split = format.split("-");
        if("01".equals(split[1]) || "02".equals(split[1]) || "03".equals(split[1])){
            format=split[0]+"年一季度";
            bzformat=split[0]+"-"+"01";
        }else if("04".equals(split[1]) || "05".equals(split[1]) || "06".equals(split[1])){
            format=split[0]+"年二季度";
            bzformat=split[0]+"-"+"04";
        }else if("07".equals(split[1]) || "08".equals(split[1]) || "09".equals(split[1])){
            format=split[0]+"年三季度";
            bzformat=split[0]+"-"+"07";
        }else if("10".equals(split[1]) || "11".equals(split[1]) || "12".equals(split[1])){
            format=split[0]+"年四季度";
            bzformat=split[0]+"-"+"10";
        }
        Date parse = null;
        try {
            parse = sdf.parse(bzformat);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//        bumaStatisticsDay.setManame(bumaHead.getBomname());
//        bumaStatisticsDay.setType("1");
////        bumaStatisticsDay.setDate2(date);
//        List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//        if(bumaStatisticsDays.isEmpty() || bumaStatisticsDays.size()<1){
//            BumaStatisticsDay param = new BumaStatisticsDay();
//            List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
//            for (BumaStatisticsDay statisticsDay : bumaStatisticsDays1) {
//                bumaStatisticsDay.setDate2(statisticsDay.getDate2());
//                bumaStatisticsDay.setDate(statisticsDay.getDate());
//                bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//            }
//        }else{
////            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setDate(format);
//            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(bumaStatisticsDays2.isEmpty()){
//                bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//            }
//        }



        List<BumawhRawBody> bumawhRawBodyList = bumaHead.getBumawhRawBodyList();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
            BumaStatisticsQuarter bumaStatisticsQuarter = new BumaStatisticsQuarter();
            bumaStatisticsQuarter.setManame(bumawhRawBody.getRawname());
            bumaStatisticsQuarter.setType("2");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(bumaStatisticsQuarter);
//            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
            if(bumaStatisticsQuarters.isEmpty() || bumaStatisticsQuarters.size()<1){
                BumaStatisticsQuarter param = new BumaStatisticsQuarter();
                List<BumaStatisticsQuarter> selectdate = bumaStatisticsQuarterMapper.selectdate(param);
//                List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
                for (BumaStatisticsQuarter statisticsQuarter : selectdate) {
                    bumaStatisticsQuarter.setDate2(statisticsQuarter.getDate2());
                    bumaStatisticsQuarter.setDate(statisticsQuarter.getDate());
                    bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsQuarter.setDate(format);
                List<BumaStatisticsQuarter> bumaStatisticsQuarters1 = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(bumaStatisticsQuarter);
                if(bumaStatisticsQuarters1.isEmpty()){
                    bumaStatisticsQuarter.setDate2(parse);
                    bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
                }
            }
        }

        List<BumawhChaBody> bumawhChaBodyList = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
            BumaStatisticsQuarter bumaStatisticsQuarter = new BumaStatisticsQuarter();
            bumaStatisticsQuarter.setManame(bumawhChaBody.getChaname());
            bumaStatisticsQuarter.setType("3");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(bumaStatisticsQuarter);
            if(bumaStatisticsQuarters.isEmpty() || bumaStatisticsQuarters.size()<1){
                BumaStatisticsQuarter param = new BumaStatisticsQuarter();
                List<BumaStatisticsQuarter> selectdate = bumaStatisticsQuarterMapper.selectdate(param);
                for (BumaStatisticsQuarter statisticsQuarter : selectdate) {
                    bumaStatisticsQuarter.setDate2(statisticsQuarter.getDate2());
                    bumaStatisticsQuarter.setDate(statisticsQuarter.getDate());
                    bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsQuarter.setDate(format);
                List<BumaStatisticsQuarter> bumaStatisticsQuarters1 = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(bumaStatisticsQuarter);
                if(bumaStatisticsQuarters1.isEmpty()){
                    bumaStatisticsQuarter.setDate2(parse);
                    bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
                }
            }
        }
        //渠道采购价维护
        List<BumawhChaBody> bumawhChaBodyList1 = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList1) {
            BumaStatisticsQuarter bumaStatisticsQuarter = new BumaStatisticsQuarter();
            bumaStatisticsQuarter.setManame(bumawhChaBody.getChaname()+"采购价");
            bumaStatisticsQuarter.setCode(bumawhChaBody.getChaname());
            bumaStatisticsQuarter.setType("1");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(bumaStatisticsQuarter);
            if(bumaStatisticsQuarters.isEmpty() || bumaStatisticsQuarters.size()<1){
                BumaStatisticsQuarter param = new BumaStatisticsQuarter();
                List<BumaStatisticsQuarter> selectdate = bumaStatisticsQuarterMapper.selectdate(param);
                for (BumaStatisticsQuarter statisticsQuarter : selectdate) {
                    bumaStatisticsQuarter.setDate(statisticsQuarter.getDate());
                    bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsQuarter.setDate(format);
                List<BumaStatisticsQuarter> bumaStatisticsQuarters1 = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(bumaStatisticsQuarter);
                if(bumaStatisticsQuarters1.isEmpty()){
                    bumaStatisticsQuarter.setDate2(parse);
                    bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
                }
            }
        }
        //ERP价维护
        List<BumawhErpmaBody> bumawhErpmaBodyList = bumaHead.getBumawhErpmaBodyList();
        for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList) {
            BumaStatisticsQuarter bumaStatisticsQuarter = new BumaStatisticsQuarter();
            bumaStatisticsQuarter.setManame(bumawhErpmaBody.getErpname());
            bumaStatisticsQuarter.setCode(bumawhErpmaBody.getErpcode());
            bumaStatisticsQuarter.setType("4");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsQuarter> bumaStatisticsQuarters = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(bumaStatisticsQuarter);
            if(bumaStatisticsQuarters.isEmpty() || bumaStatisticsQuarters.size()<1){
                BumaStatisticsQuarter param = new BumaStatisticsQuarter();
                List<BumaStatisticsQuarter> selectdate = bumaStatisticsQuarterMapper.selectdate(param);
                for (BumaStatisticsQuarter statisticsQuarter : selectdate) {
                    bumaStatisticsQuarter.setDate2(statisticsQuarter.getDate2());
                    bumaStatisticsQuarter.setDate(statisticsQuarter.getDate());
                    bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsQuarter.setDate(format);
                List<BumaStatisticsQuarter> bumaStatisticsQuarters1 = bumaStatisticsQuarterMapper.selectBumaStatisticsQuarterList(bumaStatisticsQuarter);
                if(bumaStatisticsQuarters1.isEmpty()){
                    bumaStatisticsQuarter.setDate2(parse);
                    bumaStatisticsQuarterMapper.insertBumaStatisticsQuarter(bumaStatisticsQuarter);
                }
            }
        }
    }

    public void handBumaStatisticsHalfYear(String request){
        JSONObject jsonObject = JSONObject.fromObject(request);
        //查询信息
        String id = jsonObject.optString("id");

        BumawhHead bumaHead = bumawhHeadMapper.selectBumaHeadById2(id);
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -1);
//        Date date1 = calendar.getTime();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(date);
        String bzformat = "";
        //格式时间
        String[] split = format.split("-");
        if("01".equals(split[1]) || "02".equals(split[1]) || "03".equals(split[1]) ||
                "04".equals(split[1]) || "05".equals(split[1]) || "06".equals(split[1])
        ){
            format=split[0]+"年上半年";
            bzformat=split[0]+"-"+"01";
        }else if("07".equals(split[1]) || "08".equals(split[1]) || "09".equals(split[1]) ||
        "10".equals(split[1]) || "11".equals(split[1]) || "12".equals(split[1])
        ){
            format=split[0]+"年下半年";
            bzformat=split[0]+"-"+"07";
        }
        Date parse = null;
        try {
            parse = sdf.parse(bzformat);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        BumaStatisticsDay bumaStatisticsDay = new BumaStatisticsDay();
//        bumaStatisticsDay.setManame(bumaHead.getBomname());
//        bumaStatisticsDay.setType("1");
////        bumaStatisticsDay.setDate2(date);
//        List<BumaStatisticsDay> bumaStatisticsDays = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//        if(bumaStatisticsDays.isEmpty() || bumaStatisticsDays.size()<1){
//            BumaStatisticsDay param = new BumaStatisticsDay();
//            List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
//            for (BumaStatisticsDay statisticsDay : bumaStatisticsDays1) {
//                bumaStatisticsDay.setDate2(statisticsDay.getDate2());
//                bumaStatisticsDay.setDate(statisticsDay.getDate());
//                bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//            }
//        }else{
////            bumaStatisticsDay.setDate2(date);
//            bumaStatisticsDay.setDate(format);
//            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay);
//            if(bumaStatisticsDays2.isEmpty()){
//                bumaStatisticsDayMapper.insertBumaStatisticsDay(bumaStatisticsDay);
//            }
//        }



        List<BumawhRawBody> bumawhRawBodyList = bumaHead.getBumawhRawBodyList();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
            BumaStatisticsHalfyear bumaStatisticsHalfyear = new BumaStatisticsHalfyear();
            bumaStatisticsHalfyear.setManame(bumawhRawBody.getRawname());
            bumaStatisticsHalfyear.setType("2");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsHalfyear> bumaStatisticsHalfyears = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(bumaStatisticsHalfyear);
//            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
            if(bumaStatisticsHalfyears.isEmpty() || bumaStatisticsHalfyears.size()<1){
                BumaStatisticsHalfyear param = new BumaStatisticsHalfyear();
                List<BumaStatisticsHalfyear> selectdate = bumaStatisticsHalfyearMapper.selectdate(param);
//                List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
                for (BumaStatisticsHalfyear statisticsHalfyear : selectdate) {
                    bumaStatisticsHalfyear.setDate2(statisticsHalfyear.getDate2());
                    bumaStatisticsHalfyear.setDate(statisticsHalfyear.getDate());
                    bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsHalfyear.setDate(format);
                List<BumaStatisticsHalfyear> bumaStatisticsHalfyears1 = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(bumaStatisticsHalfyear);
                if(bumaStatisticsHalfyears1.isEmpty()){
                    bumaStatisticsHalfyear.setDate2(parse);
                    bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
                }
            }
        }

        List<BumawhChaBody> bumawhChaBodyList = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
            BumaStatisticsHalfyear bumaStatisticsHalfyear = new BumaStatisticsHalfyear();
            bumaStatisticsHalfyear.setManame(bumawhChaBody.getChaname());
            bumaStatisticsHalfyear.setType("3");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsHalfyear> bumaStatisticsHalfyears = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(bumaStatisticsHalfyear);
            if(bumaStatisticsHalfyears.isEmpty() || bumaStatisticsHalfyears.size()<1){
                BumaStatisticsHalfyear param = new BumaStatisticsHalfyear();
                List<BumaStatisticsHalfyear> selectdate = bumaStatisticsHalfyearMapper.selectdate(param);
                for (BumaStatisticsHalfyear statisticsHalfyear : selectdate) {
                    bumaStatisticsHalfyear.setDate2(statisticsHalfyear.getDate2());
                    bumaStatisticsHalfyear.setDate(statisticsHalfyear.getDate());
                    bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsHalfyear.setDate(format);
                List<BumaStatisticsHalfyear> bumaStatisticsHalfyears1 = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(bumaStatisticsHalfyear);
                if(bumaStatisticsHalfyears1.isEmpty()){
                    bumaStatisticsHalfyear.setDate2(parse);
                    bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
                }
            }
        }
        //渠道采购价维护
        List<BumawhChaBody> bumawhChaBodyList1 = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList1) {
            BumaStatisticsHalfyear bumaStatisticsHalfyear = new BumaStatisticsHalfyear();
            bumaStatisticsHalfyear.setManame(bumawhChaBody.getChaname()+"采购价");
            bumaStatisticsHalfyear.setCode(bumawhChaBody.getChaname());
            bumaStatisticsHalfyear.setType("1");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsHalfyear> bumaStatisticsHalfyears = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(bumaStatisticsHalfyear);
            if(bumaStatisticsHalfyears.isEmpty() || bumaStatisticsHalfyears.size()<1){
                BumaStatisticsHalfyear param = new BumaStatisticsHalfyear();
                List<BumaStatisticsHalfyear> selectdate = bumaStatisticsHalfyearMapper.selectdate(param);
                for (BumaStatisticsHalfyear statisticsHalfyear : selectdate) {
                    bumaStatisticsHalfyear.setDate(statisticsHalfyear.getDate());
                    bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsHalfyear.setDate(format);
                List<BumaStatisticsHalfyear> bumaStatisticsHalfyears1 = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(bumaStatisticsHalfyear);
                if(bumaStatisticsHalfyears1.isEmpty()){
                    bumaStatisticsHalfyear.setDate2(parse);
                    bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
                }
            }
        }
        //ERP价维护
        List<BumawhErpmaBody> bumawhErpmaBodyList = bumaHead.getBumawhErpmaBodyList();
        for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList) {
            BumaStatisticsHalfyear bumaStatisticsHalfyear = new BumaStatisticsHalfyear();
            bumaStatisticsHalfyear.setManame(bumawhErpmaBody.getErpname());
            bumaStatisticsHalfyear.setCode(bumawhErpmaBody.getErpcode());
            bumaStatisticsHalfyear.setType("4");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsHalfyear> bumaStatisticsHalfyears = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(bumaStatisticsHalfyear);
            if(bumaStatisticsHalfyears.isEmpty() || bumaStatisticsHalfyears.size()<1){
                BumaStatisticsHalfyear param = new BumaStatisticsHalfyear();
                List<BumaStatisticsHalfyear> selectdate = bumaStatisticsHalfyearMapper.selectdate(param);
                for (BumaStatisticsHalfyear statisticsHalfyear : selectdate) {
                    bumaStatisticsHalfyear.setDate2(statisticsHalfyear.getDate2());
                    bumaStatisticsHalfyear.setDate(statisticsHalfyear.getDate());
                    bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsHalfyear.setDate(format);
                List<BumaStatisticsHalfyear> bumaStatisticsHalfyears1 = bumaStatisticsHalfyearMapper.selectBumaStatisticsHalfyearList(bumaStatisticsHalfyear);
                if(bumaStatisticsHalfyears1.isEmpty()){
                    bumaStatisticsHalfyear.setDate2(parse);
                    bumaStatisticsHalfyearMapper.insertBumaStatisticsHalfyear(bumaStatisticsHalfyear);
                }
            }
        }
    }

    public void handBumaStatisticsYear(String request){
        JSONObject jsonObject = JSONObject.fromObject(request);
        //查询信息
        String id = jsonObject.optString("id");

        BumawhHead bumaHead = bumawhHeadMapper.selectBumaHeadById2(id);
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DAY_OF_MONTH, -1);
//        Date date1 = calendar.getTime();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(date);
        //格式时间
        String[] split = format.split("-");
        format=split[0]+"年";
        String bzformat=split[0]+"-"+"01";
        Date parse = null;
        try {
            parse = sdf.parse(bzformat);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<BumawhRawBody> bumawhRawBodyList = bumaHead.getBumawhRawBodyList();
        for (BumawhRawBody bumawhRawBody : bumawhRawBodyList) {
            BumaStatisticsYear bumaStatisticsYear = new BumaStatisticsYear();
                bumaStatisticsYear.setManame(bumawhRawBody.getRawname());
                bumaStatisticsYear.setType("2");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsYear> bumaStatisticsYears = bumaStatisticsYearMapper.selectBumaStatisticsYearList(bumaStatisticsYear);
//            List<BumaStatisticsDay> bumaStatisticsDays2 = bumaStatisticsDayMapper.selectBumaStatisticsDayList(bumaStatisticsDay2);
            if(bumaStatisticsYears.isEmpty() || bumaStatisticsYears.size()<1){
                BumaStatisticsYear param = new BumaStatisticsYear();
                List<BumaStatisticsYear> selectdate = bumaStatisticsYearMapper.selectdate(param);
//                List<BumaStatisticsDay> bumaStatisticsDays1 = bumaStatisticsDayMapper.selectdate(param);
                for (BumaStatisticsYear statisticsYear : selectdate) {
                    bumaStatisticsYear.setDate2(statisticsYear.getDate2());
                    bumaStatisticsYear.setDate(statisticsYear.getDate());
                    bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsYear.setDate(format);
                List<BumaStatisticsYear> bumaStatisticsYears1 = bumaStatisticsYearMapper.selectBumaStatisticsYearList(bumaStatisticsYear);
                if(bumaStatisticsYears1.isEmpty()){
                    bumaStatisticsYear.setDate2(parse);
                    bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
                }
            }
        }

        List<BumawhChaBody> bumawhChaBodyList = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList) {
            BumaStatisticsYear bumaStatisticsYear = new BumaStatisticsYear();
            bumaStatisticsYear.setManame(bumawhChaBody.getChaname());
            bumaStatisticsYear.setType("3");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsYear> bumaStatisticsYears = bumaStatisticsYearMapper.selectBumaStatisticsYearList(bumaStatisticsYear);
            if(bumaStatisticsYears.isEmpty() || bumaStatisticsYears.size()<1){
                BumaStatisticsYear param = new BumaStatisticsYear();
                List<BumaStatisticsYear> selectdate = bumaStatisticsYearMapper.selectdate(param);
                for (BumaStatisticsYear statisticsYear : selectdate) {
                    bumaStatisticsYear.setDate2(statisticsYear.getDate2());
                    bumaStatisticsYear.setDate(statisticsYear.getDate());
                    bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsYear.setDate(format);
                List<BumaStatisticsYear> bumaStatisticsYears1 = bumaStatisticsYearMapper.selectBumaStatisticsYearList(bumaStatisticsYear);
                if(bumaStatisticsYears1.isEmpty()){
                    bumaStatisticsYear.setDate2(parse);
                    bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
                }
            }
        }
        //渠道采购价维护
        List<BumawhChaBody> bumawhChaBodyList1 = bumaHead.getBumawhChaBodyList();
        for (BumawhChaBody bumawhChaBody : bumawhChaBodyList1) {
            BumaStatisticsYear bumaStatisticsYear = new BumaStatisticsYear();
            bumaStatisticsYear.setManame(bumawhChaBody.getChaname()+"采购价");
            bumaStatisticsYear.setCode(bumawhChaBody.getChaname());
            bumaStatisticsYear.setType("1");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsYear> bumaStatisticsYears = bumaStatisticsYearMapper.selectBumaStatisticsYearList(bumaStatisticsYear);
            if(bumaStatisticsYears.isEmpty() || bumaStatisticsYears.size()<1){
                BumaStatisticsYear param = new BumaStatisticsYear();
                List<BumaStatisticsYear> selectdate = bumaStatisticsYearMapper.selectdate(param);
                for (BumaStatisticsYear statisticsYear : selectdate) {
                    bumaStatisticsYear.setDate(statisticsYear.getDate());
                    bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsYear.setDate(format);
                List<BumaStatisticsYear> bumaStatisticsYears1 = bumaStatisticsYearMapper.selectBumaStatisticsYearList(bumaStatisticsYear);
                if(bumaStatisticsYears1.isEmpty()){
                    bumaStatisticsYear.setDate2(parse);
                    bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
                }
            }
        }
        //ERP价维护
        List<BumawhErpmaBody> bumawhErpmaBodyList = bumaHead.getBumawhErpmaBodyList();
        for (BumawhErpmaBody bumawhErpmaBody : bumawhErpmaBodyList) {
            BumaStatisticsYear bumaStatisticsYear = new BumaStatisticsYear();
            bumaStatisticsYear.setManame(bumawhErpmaBody.getErpname());
            bumaStatisticsYear.setCode(bumawhErpmaBody.getErpcode());
            bumaStatisticsYear.setType("4");
//        bumaStatisticsDay.setDate2(date);
            List<BumaStatisticsYear> bumaStatisticsYears = bumaStatisticsYearMapper.selectBumaStatisticsYearList(bumaStatisticsYear);
            if(bumaStatisticsYears.isEmpty() || bumaStatisticsYears.size()<1){
                BumaStatisticsYear param = new BumaStatisticsYear();
                List<BumaStatisticsYear> selectdate = bumaStatisticsYearMapper.selectdate(param);
                for (BumaStatisticsYear statisticsYear : selectdate) {
                    bumaStatisticsYear.setDate2(statisticsYear.getDate2());
                    bumaStatisticsYear.setDate(statisticsYear.getDate());
                    bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
                }
            }else{
//                bumaStatisticsDay2.setDate2(date);
                bumaStatisticsYear.setDate(format);
                List<BumaStatisticsYear> bumaStatisticsYears1 = bumaStatisticsYearMapper.selectBumaStatisticsYearList(bumaStatisticsYear);
                if(bumaStatisticsYears1.isEmpty()){
                    bumaStatisticsYear.setDate2(parse);
                    bumaStatisticsYearMapper.insertBumaStatisticsYear(bumaStatisticsYear);
                }
            }
        }
    }

    public AjaxResult handBumaStatistics(String request){
        //后期设置只设置半年的
        //日
        handBumaStatisticsDay(request);
        //月
        handBumaStatisticsMonth(request);
        //季度
        handBumaStatisticsQuarter(request);
        //半年
        handBumaStatisticsHalfYear(request);
        //年
        handBumaStatisticsYear(request);
        return null;
    }
}
